Fastify v2 is coming to town

Image for post
Image for post
Courtesy of Rodrigo Ardilha https://unsplash.com/photos/TYtbqZdYtc8

Doing everything right at the first try is not just impossible, but also weird.
In the past months, we have gathered your feedback, issues, and needs and we worked behind the scenes to shape Fastify v2 to address all of them.

Starting today Fastify v2 will be released as Release Candidate, so you can try it and tell us if there is still something we must fix or improve before cut the final release. During the RC period, we will also update all of our official plugins to support at best Fastify v2 and use the new features to improve them. You can install it now with npm install fastify@next.

Speaking of new features and breaking changes, following you can find the list of all the major changes we did, with the link to their pull request, a brief description, and a code example.

Let us know about any compatibility and upgrade issues at https://github.com/fastify/help.

Added onError hook #1236

fastify.addHook(‘onError’, (request, reply, error, next) => {
// some code
next()
})

Move all _ prefixed properties to Symbols in next #1237 #1128

Plugins can now receive both /prefix and prefix/ #1228

const fastify = require(‘fastify’)()fastify.register(function (instance, options, next) {
// the route will be called for both ‘/english/’ and ‘/english’
instance.get(‘/’, (req, reply) => {
reply.send({ greet: ‘hello’ })
})
next()
}, { prefix: ‘/english’ })
fastify.listen(8000, function (err, address) {
if (err) throw err
console.log(`server listening on ${address}`)
})

Default error handler that logs the error #1135


fastify.setErrorHandler(function (error, request, reply) {
request.log.warn(error) // this is needed now
var statusCode = error.statusCode >= 400 ? error.statusCode : 500
reply
.code(statusCode)
.type(‘text/plain’)
.send(statusCode >= 500
? ‘Internal server error’
: error.message
)
})

Ensure Reply#serialize uses custom serializer #1163

Better error management #1168

Bumped avvio to 6.0.0. #1201

fastify.addHook('onClose', (done) => {
// some code
done()
})

Added preValidation hook #1153

fastify.addHook('preValidation', (request, reply, next) => {
// some code
next()
})

Change basePath to prefix #1183

Separate logging and reqId options #1164 #1166

Change the default plugin timeout to 10 seconds instead of 0 #1145

Pino v5 #1065

As an example, the following snippet will print all the incoming headers but the authorization.

const fastify = require(‘fastify’)({
logger: {
stream: stream,
redact: [‘req.headers.authorization’],
level: ‘info’,
serializers: {
req(req) {
return {
method: req.method,
url: req.url,
headers: req.headers,
hostname: req.hostname,
remoteAddress: req.ip,
remotePort: req.connection.remotePort
}
}
}
}
})

Updated Not found handling #1084

Hooks: always use Fastify core objects #1066

From Fastify v2 we have fixed that, and now every hook will expose the exact same signature. The middlewares will continue to expose the default Node.js core objects to allow compatibility with Express middlewares. If you are not using Express middlewares, we highly recommend using Fastify hooks.

fastify.addHook(‘onRequest’, function (request, reply, next) {
// request and reply are the core Fastify objects
next()
})
fastify.addHook(‘onResponse’, function (request, reply, next) {
// request and reply are the core Fastify objects
next()
})
fastify.use(function (req, res, next) {
// req and res are the Node core objects
next()
})

Remove `req.id` in serializer #1032

Removed url.parse and added custom querystring parser support #1270

Extended support of cross-platform builds and yarn official support #1226

Image for post
Image for post
Courtesy of lee bernd https://unsplash.com/photos/INlDrPFQIQc

Acknowledgments

More than 1700 commits, more than 90 releases, more than 450 forks, used in production by more than 8 companies, more than 260k downloads, and 87 officially recognized plugins.

All this work wouldn’t be possible without the help of our fantastic team, which we would like to thank for all their hard work!
allevo, cemremengu, delvedor, evanshortiss, lmammino, jsumners, mcollina, nwoltman, starptech, and trivikr.
We also want to thank all of our amazing contributors, for all the bug reporting, pull requests and for have believed in our project!

Fastify v2 is covered by our LTS support cycle, organized as follows: at least six months of active support plus six months of security fixes (after the release of Fastify v3), while Fastify v1 will reach EOL on June first.

Happy coding!

The Fastify Team

Written by

Fast and low overhead web framework, for Node.js — https://www.fastify.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store