UNPKG

10.7 kBMarkdownView Raw
1<h1 align="center">Fastify</h1>
2
3## Reply
4- [Reply](#reply)
5 - [Introduction](#introduction)
6 - [.code(statusCode)](#codestatuscode)
7 - [.header(key, value)](#headerkey-value)
8 - [.getHeader(key)](#getheaderkey)
9 - [.removeHeader(key)](#removeheaderkey)
10 - [.hasHeader(key)](#hasheaderkey)
11 - [.redirect(dest)](#redirectdest)
12 - [.callNotFound()](#callnotfound)
13 - [.type(contentType)](#typecontenttype)
14 - [.serializer(func)](#serializerfunc)
15 - [.sent](#sent)
16 - [.send(data)](#senddata)
17 - [Objects](#objects)
18 - [Strings](#strings)
19 - [Streams](#streams)
20 - [Buffers](#buffers)
21 - [Errors](#errors)
22 - [Type of the final payload](#type-of-the-final-payload)
23 - [Async-Await and Promises](#async-await-and-promises)
24
25<a name="introduction"></a>
26### Introduction
27The second parameter of the handler function is `Reply`.
28Reply is a core Fastify object that exposes the following functions
29and properties:
30
31- `.code(statusCode)` - Sets the status code.
32- `.status(statusCode)` - An alias for `.code(statusCode)`.
33- `.header(name, value)` - Sets a response header.
34- `.getHeader(name)` - Retrieve value of already set header.
35- `.removeHeader(key)` - Remove the value of a previously set header.
36- `.hasHeader(name)` - Determine if a header has been set.
37- `.type(value)` - Sets the header `Content-Type`.
38- `.redirect([code,] url)` - Redirect to the specified url, the status code is optional (default to `302`).
39- `.callNotFound()` - Invokes the custom not found handler.
40- `.serialize(payload)` - Serializes the specified payload using the default json serializer or using the custom serializer (if one is set) and returns the serialized payload.
41- `.serializer(function)` - Sets a custom serializer for the payload.
42- `.send(payload)` - Sends the payload to the user, could be a plain text, a buffer, JSON, stream, or an Error object.
43- `.sent` - A boolean value that you can use if you need to know if `send` has already been called.
44- `.res` - The [`http.ServerResponse`](https://nodejs.org/dist/latest/docs/api/http.html#http_class_http_serverresponse) from Node core.
45- `.log` - the logger instance of the incoming request
46
47```js
48fastify.get('/', options, function (request, reply) {
49 // Your code
50 reply
51 .code(200)
52 .header('Content-Type', 'application/json; charset=utf-8')
53 .send({ hello: 'world' })
54})
55```
56
57Additionally, `Reply` provides access to the context of the request:
58
59```js
60fastify.get('/', {config: {foo: 'bar'}}, function (request, reply) {
61 reply.send('handler config.foo = ' + reply.context.config.foo)
62})
63```
64
65<a name="code"></a>
66### .code(statusCode)
67If not set via `reply.code`, the resulting `statusCode` will be `200`.
68
69<a name="header"></a>
70### .header(key, value)
71Sets a response header. If the value is omitted or undefined it is coerced
72to `''`.
73
74For more information, see [`http.ServerResponse#setHeader`](https://nodejs.org/dist/latest/docs/api/http.html#http_response_setheader_name_value).
75
76<a name="getHeader"></a>
77### .getHeader(key)
78Retrieves the value of a previously set header.
79```js
80reply.header('x-foo', 'foo')
81reply.getHeader('x-foo') // 'foo'
82```
83
84<a name="getHeader"></a>
85### .removeHeader(key)
86
87Remove the value of a previously set header.
88```js
89reply.header('x-foo', 'foo')
90reply.removeHeader('x-foo')
91reply.getHeader('x-foo') // undefined
92```
93
94<a name="hasHeader"></a>
95### .hasHeader(key)
96Returns a boolean indicating if the specified header has been set.
97
98<a name="redirect"></a>
99### .redirect(dest)
100Redirects a request to the specified url, the status code is optional, default to `302` (if status code is not already set by calling `code`).
101```js
102reply.redirect('/home')
103```
104
105<a name="call-not-found"></a>
106### .callNotFound()
107Invokes the custom not found handler.
108```js
109reply.callNotFound()
110```
111
112<a name="type"></a>
113### .type(contentType)
114Sets the content type for the response.
115This is a shortcut for `reply.header('Content-Type', 'the/type')`.
116
117```js
118reply.type('text/html')
119```
120
121<a name="serializer"></a>
122### .serializer(func)
123`.send()` will by default JSON-serialize any value that is not one of: `Buffer`, `stream`, `string`, `undefined`, `Error`. If you need to replace the default serializer with a custom serializer for a particular request, you can do so with the `.serializer()` utility. Be aware that if you are using a custom serializer, you must set a custom `'Content-Type'` header.
124
125```js
126reply
127 .header('Content-Type', 'application/x-protobuf')
128 .serializer(protoBuf.serialize)
129```
130
131Note that you don't need to use this utility inside a `handler` because Buffers, streams, and strings (unless a serializer is set) are considered to already be serialized.
132
133```js
134reply
135 .header('Content-Type', 'application/x-protobuf')
136 .send(protoBuf.serialize(data))
137```
138
139See [`.send()`](#send) for more information on sending different types of values.
140
141<a name="sent"></a>
142### .sent
143
144As the name suggests, `.sent` is a property to indicate if
145a response has been sent via `reply.send()`.
146
147In case a route handler is defined as an async function or it
148returns a promise, it is possible to set `reply.sent = true`
149to indicate that the automatic invocation of `reply.send()` once the
150handler promise resolve should be skipped. By setting `reply.sent =
151true`, an application claims full responsibility of the low-level
152request and response. Moreover, hooks will not be invoked.
153
154As an example:
155
156```js
157app.get('/', (req, reply) => {
158 reply.sent = true
159 reply.res.end('hello world')
160
161 return Promise.resolve('this will be skipped')
162})
163```
164
165If the handler rejects, the error will be logged.
166
167<a name="send"></a>
168### .send(data)
169As the name suggests, `.send()` is the function that sends the payload to the end user.
170
171<a name="send-object"></a>
172#### Objects
173As noted above, if you are sending JSON objects, `send` will serialize the object with [fast-json-stringify](https://www.npmjs.com/package/fast-json-stringify) if you set an output schema, otherwise `JSON.stringify()` will be used.
174```js
175fastify.get('/json', options, function (request, reply) {
176 reply.send({ hello: 'world' })
177})
178```
179
180<a name="send-string"></a>
181#### Strings
182If you pass a string to `send` without a `Content-Type`, it will be sent as `text/plain; charset=utf-8`. If you set the `Content-Type` header and pass a string to `send`, it will be serialized with the custom serializer if one is set, otherwise it will be sent unmodified (unless the `Content-Type` header is set to `application/json; charset=utf-8`, in which case it will be JSON-serialized like an object — see the section above).
183```js
184fastify.get('/json', options, function (request, reply) {
185 reply.send('plain string')
186})
187```
188
189<a name="send-streams"></a>
190#### Streams
191*send* can also handle streams out of the box, internally uses [pump](https://www.npmjs.com/package/pump) to avoid leaks of file descriptors. If you are sending a stream and you have not set a `'Content-Type'` header, *send* will set it at `'application/octet-stream'`.
192```js
193fastify.get('/streams', function (request, reply) {
194 const fs = require('fs')
195 const stream = fs.createReadStream('some-file', 'utf8')
196 reply.send(stream)
197})
198```
199
200<a name="send-buffers"></a>
201#### Buffers
202If you are sending a buffer and you have not set a `'Content-Type'` header, *send* will set it to `'application/octet-stream'`.
203```js
204const fs = require('fs')
205fastify.get('/streams', function (request, reply) {
206 fs.readFile('some-file', (err, fileBuffer) => {
207 reply.send(err || fileBuffer)
208 })
209})
210```
211
212<a name="errors"></a>
213#### Errors
214If you pass to *send* an object that is an instance of *Error*, Fastify will automatically create an error structured as the following:
215```js
216{
217 error: String // the http error message
218 message: String // the user error message
219 statusCode: Number // the http status code
220}
221```
222You can add some custom property to the Error object, such as `statusCode` and `headers`, that will be used to enhance the http response.<br>
223*Note: If you are passing an error to `send` and the statusCode is less than 400, Fastify will automatically set it at 500.*
224
225Tip: you can simplify errors by using the [`http-errors`](https://npm.im/http-errors) module or [`fastify-sensible`](https://github.com/fastify/fastify-sensible) plugin to generate errors:
226
227```js
228fastify.get('/', function (request, reply) {
229 reply.send(httpErrors.Gone())
230})
231```
232
233If you want to completely customize the error handling, checkout [`setErrorHandler`](https://github.com/fastify/fastify/blob/master/docs/Server.md#seterrorhandler) API.<br>
234*Note: you are responsibile for logging when customizing the error handler*
235
236API:
237
238```js
239fastify.setErrorHandler(function (error, request, reply) {
240 request.log.warn(error)
241 var statusCode = error.statusCode >= 400 ? error.statusCode : 500
242 reply
243 .code(statusCode)
244 .type('text/plain')
245 .send(statusCode >= 500 ? 'Internal server error' : error.message)
246})
247```
248
249The not found errors generated by the router will use the [`setNotFoundHandler`](https://github.com/fastify/fastify/blob/master/docs/Server.md#setnotfoundhandler)
250
251API:
252
253```js
254fastify.setNotFoundHandler(function (request, reply) {
255 reply
256 .code(404)
257 .type('text/plain')
258 .send('a custom not found')
259})
260```
261
262<a name="payload-type"></a>
263#### Type of the final payload
264The type of the sent payload (after serialization and going through any [`onSend` hooks](https://github.com/fastify/fastify/blob/master/docs/Hooks.md#the-onsend-hook)) must be one of the following types, otherwise an error will be thrown:
265
266- `string`
267- `Buffer`
268- `stream`
269- `undefined`
270- `null`
271
272<a name="async-await-promise"></a>
273#### Async-Await and Promises
274Fastify natively handles promises and supports async-await.<br>
275*Note that in the following examples we are not using reply.send.*
276```js
277fastify.get('/promises', options, function (request, reply) {
278 return new Promise(function (resolve) {
279 setTimeout(resolve, 200, { hello: 'world' })
280 })
281})
282
283fastify.get('/async-await', options, async function (request, reply) {
284 var res = await new Promise(function (resolve) {
285 setTimeout(resolve, 200, { hello: 'world' })
286 })
287 return res
288})
289```
290
291Rejected promises default to a `500` HTTP status code. Reject the promise, or `throw` in an `async function`, with an object that has `statusCode` (or `status`) and `message` properties to modify the reply.
292
293```js
294fastify.get('/teapot', async function (request, reply) => {
295 const err = new Error()
296 err.statusCode = 418
297 err.message = 'short and stout'
298 throw err
299})
300```
301
302If you want to know more please review [Routes#async-await](https://github.com/fastify/fastify/blob/master/docs/Routes.md#async-await).