Reply
Reply
- Reply
- Introduction
- .code(statusCode)
- .elapsedTime
- .statusCode
- .server
- .header(key, value)
- .headers(object)
- .getHeader(key)
- .getHeaders()
- .removeHeader(key)
- .hasHeader(key)
- .trailer(key, function)
- .hasTrailer(key)
- .removeTrailer(key)
- .redirect(dest, [code ,])
- .callNotFound()
- .getResponseTime()
- .type(contentType)
- .getSerializationFunction(schema | httpStatus, [contentType])
- .compileSerializationSchema(schema, [httpStatus], [contentType])
- .serializeInput(data, [schema | httpStatus], [httpStatus], [contentType])
- .serializer(func)
- .raw
- .sent
- .hijack()
- .send(data)
- .then(fulfilled, rejected)
Introduction
The second parameter of the handler function is Reply
. Reply is a core Fastify
object that exposes the following functions and properties:
.code(statusCode)
- Sets the status code..status(statusCode)
- An alias for.code(statusCode)
..statusCode
- Read and set the HTTP status code..elapsedTime
- Returns the amount of time passed since the request was received by Fastify..server
- A reference to the fastify instance object..header(name, value)
- Sets a response header..headers(object)
- Sets all the keys of the object as response headers..getHeader(name)
- Retrieve value of already set header..getHeaders()
- Gets a shallow copy of all current response headers..removeHeader(key)
- Remove the value of a previously set header..hasHeader(name)
- Determine if a header has been set..trailer(key, function)
- Sets a response trailer..hasTrailer(key)
- Determine if a trailer has been set..removeTrailer(key)
- Remove the value of a previously set trailer..type(value)
- Sets the headerContent-Type
..redirect(dest, [code,])
- Redirect to the specified URL, the status code is optional (defaults to302
)..callNotFound()
- Invokes the custom not found handler..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..getSerializationFunction(schema | httpStatus, [contentType])
- Returns the serialization function for the specified schema or http status, if any of either are set..compileSerializationSchema(schema, [httpStatus], [contentType])
- Compiles the specified schema and returns a serialization function using the default (or customized)SerializerCompiler
. The optionalhttpStatus
is forwarded to theSerializerCompiler
if provided, default toundefined
..serializeInput(data, schema, [,httpStatus], [contentType])
- Serializes the specified data using the specified schema and returns the serialized payload. If the optionalhttpStatus
, andcontentType
are provided, the function will use the serializer function given for that specific content type and HTTP Status Code. Default toundefined
..serializer(function)
- Sets a custom serializer for the payload..send(payload)
- Sends the payload to the user, could be a plain text, a buffer, JSON, stream, or an Error object..sent
- A boolean value that you can use if you need to know ifsend
has already been called..hijack()
- interrupt the normal request lifecycle..raw
- Thehttp.ServerResponse
from Node core..log
- The logger instance of the incoming request..request
- The incoming request..getResponseTime()
- Deprecated, returns the amount of time passed since the request was received by Fastify..context
- Deprecated, access the Request's context property.
fastify.get('/', options, function (request, reply) {
// Your code
reply
.code(200)
.header('Content-Type', 'application/json; charset=utf-8')
.send({ hello: 'world' })
})
Additionally, Reply
provides access to the context of the request:
fastify.get('/', {config: {foo: 'bar'}}, function (request, reply) {
reply.send('handler config.foo = ' + reply.context.config.foo)
})
.code(statusCode)
If not set via reply.code
, the resulting statusCode
will be 200
.
.elapsedTime
Invokes the custom response time getter to calculate the amount of time passed since the request was received by Fastify.
Note that unless this function is called in the onResponse
hook it will always return 0
.
const milliseconds = reply.elapsedTime
.statusCode
This property reads and sets the HTTP status code. It is an alias for
reply.code()
when used as a setter.
if (reply.statusCode >= 299) {
reply.statusCode = 500
}
.server
The Fastify server instance, scoped to the current encapsulation context.
fastify.decorate('util', function util () {
return 'foo'
})
fastify.get('/', async function (req, rep) {
return rep.server.util() // foo
})
.header(key, value)
Sets a response header. If the value is omitted or undefined, it is coerced to
''
.
Note: the header's value must be properly encoded using
encodeURI
or similar modules such asencodeurl
. Invalid characters will result in a 500TypeError
response.
For more information, see
http.ServerResponse#setHeader
.