Categories
Fastify

Server-Side Development with Fastify — Errors and Body Parser

Fastify is a small Node framework for developing back end web apps.

In this article, we’ll look at how to create back end apps with Fastify.

Errors

We can throw various kinds of errors in our Fastify app.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/' ,async () => {
  throw new Error('error')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Then when we go to / , we get a 500 response.

Fastify includes the following errors codes:

  • FST_ERR_BAD_URL — The router received an invalid URL.
  • FST_ERR_CTP_ALREADY_PRESENT — The parser for this content type was already registered.
  • FST_ERR_CTP_INVALID_TYPE — The Content-Type should be a string.
  • FST_ERR_CTP_EMPTY_TYPE — The content type cannot be an empty string.
  • FST_ERR_CTP_INVALID_HANDLER — An invalid handler was passed for the content type.
  • FST_ERR_CTP_INVALID_PARSE_TYPE — The provided parse type is not supported. Accepted values are string or buffer.
  • FST_ERR_CTP_BODY_TOO_LARGE — The request body is larger than the provided limit.
  • FST_ERR_CTP_INVALID_MEDIA_TYPE — The received media type is not supported (i.e. there is no suitable Content-Type parser for it).
  • FST_ERR_CTP_INVALID_CONTENT_LENGTH — Request body size did not match Content-Length.
  • FST_ERR_DEC_ALREADY_PRESENT — A decorator with the same name is already registered.
  • FST_ERR_DEC_MISSING_DEPENDENCY — The decorator cannot be registered due to a missing dependency.
  • FST_ERR_HOOK_INVALID_TYPE — The hook name must be a string.
  • FST_ERR_HOOK_INVALID_HANDLER — The hook callback must be a function.
  • FST_ERR_LOG_INVALID_DESTINATION — The logger accepts either a ‘stream’ or a ‘file’ as the destination.
  • FST_ERR_REP_ALREADY_SENT — A response was already sent.
  • FST_ERR_SEND_INSIDE_ONERR — You cannot use send inside the onError hook.
  • FST_ERR_REP_INVALID_PAYLOAD_TYPE — Reply payload can either be a string or a Buffer.
  • FST_ERR_SCH_MISSING_ID — The schema provided does not have $id property.
  • FST_ERR_SCH_ALREADY_PRESENT — A schema with the same $id already exists.
  • FST_ERR_SCH_VALIDATION_BUILD — The JSON schema provided for validation to a route is not valid.
  • FST_ERR_SCH_SERIALIZATION_BUILD — The JSON schema provided for serialization of a route response is not valid.
  • FST_ERR_PROMISE_NOT_FULLFILLED — A promise may not be fulfilled with undefined when statusCode is not 204.
  • FST_ERR_SEND_UNDEFINED_ERR — Undefined error has occurred.

Content-Type Parser

We can add parsers for different content types.

For instance, we can write:

const fastify = require('fastify')({})

fastify.addContentTypeParser('application/json', { parseAs: 'string' }, function (req, body, done) {
  try {
    const json = JSON.parse(body)
    done(null, json)
  } catch (err) {
    err.statusCode = 400
    done(err, undefined)
  }
})

fastify.post('/', async () => {
  return 'success'
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to add our own JSON request body parser.

We call fastify.addContentTypeParser with the 'application/json' argument to set the JSON.

Then we parse the JSON with JSON.parse .

We can also add a catch-all body parser with the '*' argument.

For example, we can write:

const fastify = require('fastify')({})

fastify.addContentTypeParser('application/json', { parseAs: 'string' }, function (req, body, done) {
  try {
    const json = JSON.parse(body)
    done(null, json)
  } catch (err) {
    err.statusCode = 400
    done(err, undefined)
  }
})

fastify.addContentTypeParser('*', function (request, payload, done) {
  let data = ''
  payload.on('data', chunk => { data += chunk })
  payload.on('end', () => {
    done(null, data)
  })
})

fastify.post('/', async () => {
  return 'success'
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to get the data chunks and concatenate it to the data .

Conclusion

We can throw errors in route handlers and add our own body parser into our own Fastify app.

Categories
Fastify

Server-Side Development with Fastify — Async and Await and Requests

Fastify is a small Node framework for developing back end web apps.

In this article, we’ll look at how to create back end apps with Fastify.

Async-Await and Promises

We can use async functions with our route handlers and send responses with it.

For example, we can write:

const fastify = require('fastify')({})
const { promisify } = require('util');
const delay = promisify(setTimeout)

fastify.get('/', async function (request, reply) {
  await delay(200)
  return { hello: 'world' }
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We use an async function as our route handler, so we can await promises.

Then we just return the response body to send the response.

We can throw responses with an Error instance.

For instance, we can write:

const fastify = require('fastify')({})
const { promisify } = require('util');
const delay = promisify(setTimeout)

fastify.get('/', async function (request, reply) {
  const err = new Error()
  err.statusCode = 418
  err.message = 'short and stout'
  throw err
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We can also throw an object by writing:

const fastify = require('fastify')({})
const { promisify } = require('util');
const delay = promisify(setTimeout)

fastify.get('/', async function (request, reply) {
  throw { statusCode: 418, message: 'short and stout' }
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Request

The request object has various properties.

They include:

  • query – the parsed query string
  • body – the body
  • params – the params matching the URL
  • headers – the headers
  • raw – the incoming HTTP request from Node core
  • id – the request ID
  • log – the logger instance of the incoming request
  • ip – the IP address of the incoming request
  • ips – an array of the IP addresses in the X-Forwarded-For header of the incoming request. It’s available only when the trustProxy option is enabled
  • hostname – the hostname of the incoming request
  • protocol – the protocol of the incoming request (https or http)
  • method – the method of the incoming request
  • url – the URL of the incoming request
  • routerMethod – the method defined for the router that is handling the request
  • routerPath – the path pattern defined for the router that is handling the request
  • is404true if the request is being handled by 404 handlers, false if it is not
  • socket – the underlying connection of the incoming request

We can access them by sitting:

const fastify = require('fastify')({})
const { promisify } = require('util');
const delay = promisify(setTimeout)

fastify.post('/:params', options, function (request, reply) {
  console.log(request.body)
  console.log(request.query)
  console.log(request.params)
  console.log(request.headers)
  console.log(request.raw)
  console.log(request.id)
  console.log(request.ip)
  console.log(request.ips)
  console.log(request.hostname)
  console.log(request.protocol)
  request.log.info('some info')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Conclusion

We can send responses and get request data with Fastify.

Categories
Fastify

Server-Side Development with Fastify — Sending Responses

Fastify is a small Node framework for developing back end web apps.

In this article, we’ll look at how to create back end apps with Fastify.

Strings Responses

We can send a string response with the reply.send method:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.send('plain string')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Stream Response

We can send a stream response with reply.send .

For instance, we can write:

index.js

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  const fs = require('fs')
  const stream = fs.createReadStream('some-file', 'utf8')
  reply.send(stream)
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

some-file

foo

Then we see foo as our response when we go to the / route.

Buffer Response

We can send a buffer response with the fs.readFile method.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  fs.readFile('some-file', (err, fileBuffer) => {
    reply.send(err || fileBuffer)
  })
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to send the buffer response in the fs.readFile callback.

Errors

We can send errors with the http-errors library.

For instance, we can write:

const fastify = require('fastify')({})
const httpErrors = require('http-errors')

fastify.get('/', function(req, reply) {
  reply.send(httpErrors.Gone())
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to send the 410 HTTP gone status code.

We can also add an error handler to check for the type of error raised and then send a response.

For instance, we can write:

const fastify = require('fastify')({})
const httpErrors = require('http-errors')

fastify.setErrorHandler(function (error, request, reply) {
  request.log.warn(error)
  const statusCode = error.statusCode >= 400 ? error.statusCode : 500
  reply
    .code(statusCode)
    .type('text/plain')
    .send(statusCode >= 500 ? 'Internal server error' : error.message)
})

fastify.get('/', function(req, reply) {
  reply.send(httpErrors.Gone())
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We get the error status code with the error.statusCode property.

Then we call reply.code to set the status code.

type sets the content type. send sends the response with the content of the argument.

We can also write:

const fastify = require('fastify')({})
const httpErrors = require('http-errors')

fastify.setNotFoundHandler(function (request, reply) {
  reply
    .code(404)
    .type('text/plain')
    .send('a custom not found')
})

fastify.get('/', function(req, reply) {
  reply.callNotFound()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to send the response.

We call callNotFound to call the not found handler.

Conclusion

We can send various kinds of HTTP responses with Fastify.

Categories
Fastify

Server-Side Development with Fastify — Changing and Sending Responses

Fastify is a small Node framework for developing back end web apps.

In this article, we’ll look at how to create back end apps with Fastify.

Not Found Response

We can call callNotFound to invoke the custom not found handler.

For example, we can write:

const fastify = require('fastify')({})

fastify.setNotFoundHandler({
  preValidation: (req, reply, done) => {
    done()
  },
  preHandler: (req, reply, done) => {
    done()
  }
}, function (request, reply) {
  reply.send('not found')
})

fastify.get('/', function(req, reply) {
  reply.callNotFound()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We call setNotFoundHandler to add the not found handler.

Then we call reply.callNotFound in our event handler to redirect to the not found handler.

So we see not found as the response of the / route.

Response Time

We can get the response time with the getResponseTime method.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  const milliseconds = reply.getResponseTime()
  reply.send(milliseconds)
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We call it in our handler to get the response time.

Response Content-Type

We can set the response Content-Type header with the reply.type method.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.type('text/html').send()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Then we set the Content-Type response header with the reply.type method.

Raw Response

We can send a raw response with the reply.raw property.

For instance, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.setCookie('session', 'value', { secure: false })
  reply.raw.writeHead(200, { 'Content-Type': 'text/plain' })
  reply.raw.write('ok')
  reply.raw.end()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We call setCookie to set our response cookie.

reply.raw.writeHeader adds the response header.

reply.raw.write writes the response body.

reply.raw.end ends the response.

Set if a Response is Sent

We can set the sent property of the response.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.sent = true
  reply.raw.end('hello world')
  return Promise.resolve('this will be skipped')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to set the reply.sent to true to mark the response as sent.

Send a Response

We can send the response with the reply.send method.

For instance, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.send({ hello: 'world' })
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to send the response.

Conclusion

We can set various parts of a response and send it with Fastify.

Categories
Fastify

Server-Side Development with Fastify — Responses

Fastify is a small Node framework for developing back end web apps.

In this article, we’ll look at how to create back end apps with Fastify.

Reply Object

Then reply object lets us send HTTP responses to the client.

We can set the status code with the code method.

For instance, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply
    .code(200)
    .header('Content-Type', 'application/json; charset=utf-8')
    .send({ hello: 'world' })
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to send a 200 response with the code method.

We can check the status code with the statusCode method.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  if (reply.statusCode >= 299) {
    reply.statusCode = 500
  }
  reply.send()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

to set the statusCode to 500.

Response Headers

We can set the headers of the response with the reply.headers method.

For instance, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.headers({
    'x-foo': 'foo',
    'x-bar': 'bar'
  })
  .send()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Then we send the x-foo and x-bar headers.

We can get the header with the getHeader method.

For instance, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.header('x-foo', 'foo')
  console.log(reply.getHeader('x-foo'))
  reply.send()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We call getHeader to get the header.

Also, we can get all the headers with the getHeaders method:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.header('x-foo', 'foo')
  reply.header('x-bar', 'bar')
  reply.raw.setHeader('x-foo', 'foo2')
  console.log(reply.getHeaders())
  reply.send()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

To remove a header, we call removeHeader to do so:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.header('x-foo', 'foo')
  reply.removeHeader('x-foo')
  console.log(reply.getHeader('x-foo'))
  reply.send()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We called removeHeader to remove the x-foo header, so when we call getHeader on it, we get undefined .

Redirect

We can redirect to a different route with the redirect method.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/home', function(req, reply) {
  reply.send('home')
})

fastify.get('/', function(req, reply) {
  reply.redirect('/home')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Then we redirect to the /home route.

Also, we can add the status code by writing:

const fastify = require('fastify')({})

fastify.get('/home', function(req, reply) {
  reply.send('home')
})

fastify.get('/', function(req, reply) {
  reply.code(303).redirect('/home')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

or:

const fastify = require('fastify')({})

fastify.get('/home', function(req, reply) {
  reply.send('home')
})

fastify.get('/', function(req, reply) {
  reply.redirect(303, '/home')
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Conclusion

We can send responses to the client with Fastify.