What is fastify?
Fastify is a fast and low overhead web framework for Node.js. It is highly performant and provides an extensive plugin architecture, making it suitable for building a wide range of server-side applications and services.
What are fastify's main functionalities?
Web Server
Fastify allows you to create a web server that can handle HTTP requests and send responses. The above code demonstrates setting up a simple server that responds with JSON when the root route is accessed.
const fastify = require('fastify')({ logger: true });
fastify.get('/', async (request, reply) => {
return { hello: 'world' };
});
fastify.listen(3000, (err, address) => {
if (err) throw err;
fastify.log.info(`server listening on ${address}`);
});
Route Shorthand Methods
Fastify provides shorthand methods for different HTTP methods like GET, POST, etc. This makes it easy to define routes for various request types.
fastify.get('/example', (request, reply) => {
reply.send({ message: 'This is a GET request' });
});
fastify.post('/example', (request, reply) => {
reply.send({ message: 'This is a POST request' });
});
Schema Validation
Fastify supports schema validation for request payloads, query strings, and parameters using JSON Schema. This ensures that the data received is in the expected format.
const schema = {
body: {
type: 'object',
required: ['name'],
properties: {
name: { type: 'string' },
age: { type: 'number' }
}
}
};
fastify.post('/user', { schema }, (request, reply) => {
// Handle request knowing that the body has been validated against the schema
});
Plugins
Fastify has a powerful plugin system that allows you to extend its core functionality. Plugins can add new features, routes, services, and decorators to the Fastify instance.
const myPlugin = async (fastify, options) => {
fastify.decorate('utility', () => {
return 'something useful';
});
};
fastify.register(myPlugin);
// Now you can use fastify.utility() in your application
Lifecycle Hooks
Fastify provides lifecycle hooks that can be used to execute code at various stages of the request/response cycle, such as onRequest, preHandler, onResponse, etc.
fastify.addHook('onRequest', (request, reply, done) => {
// Perform some operations before the request handler is executed
done();
});
Other packages similar to fastify
express
Express is one of the most popular web frameworks for Node.js. It is known for its simplicity and minimalism. Compared to Fastify, Express has a larger ecosystem and community but may not be as performant due to its less optimized architecture.
koa
Koa is a web framework designed by the creators of Express, aiming to be a smaller, more expressive, and more robust foundation for web applications and APIs. Koa uses async functions to eliminate callbacks and improve error handling. It is less opinionated than Fastify and has a smaller footprint.
hapi
Hapi is a rich framework for building applications and services, known for its powerful plugin system. It is designed to be more configurable and to provide a richer set of features out of the box compared to Fastify, which can make it heavier and potentially slower.
restify
Restify is a Node.js web service framework optimized for building semantically correct RESTful web services ready for production use at scale. Restify is similar to Fastify in terms of performance but is more focused on API creation than being a general-purpose web framework.
Fastify
Extremely fast node.js web framework, inspired by
Express, Hapi and Restify.
Fastify is alpha software in active development, feel free to
contribute!
Install
npm install fastify --save
Usage
'use strict'
const fastify = require('fastify')()
const schema = {
out: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
}
fastify
.get('/', schema, function (req, reply) {
reply.send({ hello: 'world' })
})
.get('/no-schema', function (req, reply) {
reply.send({ hello: 'world' })
})
.post('/', schema, function (req, reply) {
reply.send({ hello: 'world' })
})
fastify.listen(8000, function (err) {
if (err) {
throw err
}
console.log(`server listening on ${fastify.server.address().port}`)
})
Benchmarks
As far as we know, it is one of the fastest web frameworks in town:
- Hapi: 2200 req/sec
- Restify: 6133 req/sec
- Express: 8534 req/sec
- Koa: 9640 req/sec
- Fastify: 19580 req/sec
All benchmarks where average taken over 5 seconds, on the second run of autocannon -c 100 -d 5 -p 10 localhost:3000
.
## API
fastify(req, res)
Returns a new fastify instance, which is a function with some method
attached. req
and res
are the request and response objects from Node
Core.
const fastify = require('fastify')()
fastify.listen(8000, function (err) {
if (err) {
throw err
}
console.log(`server listening on ${fastify.server.address().port}`)
})
If you need an HTTPS
server, pass an option object with the keys to the fastify constructor.
const fastify = require('fastify')({
https: {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
}
})
fastify.server
The Node core server object.
fastify.ready(callback)
Function called when all the plugins has been loaded.
Emitted by boot-in-the-arse.
fastify.listen(port, [callback])
Starts the server on the given port after all the plugins are loaded, internally waits for the .ready()
event.
The callback is the same as the Node core.
fastify.route(options)
Options:
-
method
: currently it supports 'DELETE'
, 'GET'
, 'HEAD'
, 'PATCH'
, 'POST'
, 'PUT'
and 'OPTIONS'
.
-
url
: the path of the url to match this route, it uses
wayfarer as a router.
-
schema
: an object containing the schemas for the request and response. They need to be in
JSON Schema format:
payload
: validates the body of the request if it is a POST or a
PUT. It uses ajv.querystring
: validates the querystring. It uses ajv.params
: validates the params. It uses ajv.out
: filter and generate a schema for the response, setting a
schema allows us to have 10-20% more throughput. It uses
fast-json-stringify.
-
handler(request, reply)
: the function that will handle this request.
request
is defined in Request.
reply
is defined in Reply.
Example:
fastify.route({
method: 'GET',
url: '/',
schema: {
out: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
},
handler: function (request, reply) {
reply.send({ hello: 'world' })
}
})
The handler can also return a Promise, and it supports async/await:
fastify.route({
method: 'GET',
url: '/',
schema: {
out: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
},
handler: async function (request) {
var res = await new Promise(function (resolve) {
setTimeout(resolve, 200, { hello: 'world' })
})
return res
}
})
Request
An object including the following properties:
query
- the parsed querystringbody
- the bodyparams
- the params matching the URLreq
- the incoming HTTP request from Node core
Reply
An object that exposes three APIs.
.send(payload)
- Sends the payload to the user, could be a plain text, JSON, stream, or an Error object..code(statusCode)
- Sets the status code (default to 200)..header(name, value)
- Sets the headers.
Example:
fastify.get('/', schema, function (request, reply) {
reply
.code(200)
.header('Content-Type', 'application/json')
.send({ hello 'world' })
})
Reply.send() accepts also Promises:
fastify.get('/', schema, function (request, reply) {
const promise = new Promise(function (resolve, reject) {
if (condition) {
resolve({ hello: 'world' })
} else {
reject(new Error('some error'))
}
})
reply
.code(200)
.header('Content-Type', 'application/json')
.send(promise)
})
To send a stream, just pass it as a parameter to .send(), the default Content-Type
for the streams is application/octet-stream
. Pump is used to pipe the streams.
fastify.get('/', schema, function (request, reply) {
const fs = require('fs')
const stream = fs.createReadStream('some-file', 'utf8')
reply.send(stream)
})
fastify.get(path, [schema], handler)
Calls route with the given path, schemas and handler, setting
up the GET
method.
fastify.post(path, [schema], handler)
Calls route with the given path, schemas and handler, setting
up the POST
method.
fastify.put(path, [schema], handler)
Calls route with the given path, schemas and handler, setting
up the PUT
method.
fastify.delete(path, [schema], handler)
Calls route with the given path, schemas and handler, setting
up the DELETE
method.
fastify.head(path, [schema], handler)
Calls route with the given path, schemas and handler, setting
up the HEAD
method.
fastify.patch(path, [schema], handler)
Calls route with the given path, schemas and handler, setting
up the PATCH
method.
fastify.options(path, [schema], handler)
Calls route with the given path, schemas and handler, setting
up the OPTIONS
method.
fastify.register(plugin, [options], [callback])
Used to register one or more plugins.
plugin
can be a single function or an array of functions.
In case of the array of functions, the same options object and callback will be passed to them.
boot-in-the-arse is used to load the plugins.
Example:
const fastify = require('fastify')()
fastify.register(require('./plugin'), function (err) {
if (err) throw err
})
const opts = {
hello: 'world',
something: true
}
fastify.register([
require('./another-plugin'),
require('./yet-another-plugin')
], opts, function (err) {
if (err) throw err
})
fastify.listen(8000, function (err) {
if (err) {
throw err
}
console.log(`server listening on ${fastify.server.address().port}`)
})
module.exports = function (fastify, options, next) {
fastify.get('/', schema, function (req, reply) {
reply.send({ hello: 'world' })
})
next()
}
Logging
Since Fastify is really focused on performances, we choose the best logger to achieve the goal. Pino!
By default Fastify uses pino-http as logger, with the log level setted to 'fatal'
.
If you want to pass some options to the logger, just pass the logger option to fastify.
You can find all the options here. If you want to pass a custom stream to the Pino instance, just add the stream field to the logger object.
const split = require('split2')
const stream = split(JSON.parse)
const fastify = require('fastify')({
logger: {
level: 'info',
stream: stream
}
})
The Team
Matteo Collina
https://github.com/mcollina
https://www.npmjs.com/~matteo.collina
https://twitter.com/matteocollina
Tomas Della Vedova
https://github.com/delvedor
https://www.npmjs.com/~delvedor
https://twitter.com/delvedor
Acknowledgements
This project was kindly sponsored by nearForm.
License
Licensed under MIT.