Generating responses
Nest.js provides two approaches for generating responses.
Standard approach
Using the standard and recommended approach, which has been available since Nest.js 4, Nest.js will automatically serialize the JavaScript object or array returned from the handler method to JSON and send it in the response body. If a string is returned, Nest.js will just send the string without serializing it to JSON.
The default response status code is 200, expect for POST requests, which uses 201. The response code for can easily be changed for a handler method by using the @HttpCode(...)
decorator. For example:
@
HttpCode
(
204
)
@
Post
()
create() {
// This handler will return a 204 status response
}
Express approach
An alternate approach to generating responses in Nest.js is to use a response object directly. You can ask Nest.js to inject a response object into a handler method using the @Res()
decorator. Nest.js uses express response objects].
You can rewrite the response handler...