Quick start

Get fastify with NPM:

npm install fastify

Then create server.js and add the following content:

async/await // Require the framework and instantiate it const fastify = require('fastify')({ logger: true }) // Declare a route fastify.get('/', (request, reply) => { reply.send({ hello: 'world' }) }) // Run the server! fastify.listen(3000, (err) => { if (err) { fastify.log.error(err) process.exit(1) } fastify.log.info(`server listening on ${fastify.server.address().port}`) }) // Require the framework and instantiate it const fastify = require('fastify')({ logger: true }) // Declare a route fastify.get('/', async (request, reply) => { return { hello: 'world' } }) // Run the server! const start = async () => { try { await fastify.listen(3000) fastify.log.info(`server listening on ${fastify.server.address().port}`) } catch (err) { fastify.log.error(err) process.exit(1) } } start()

Finally, launch the server with:

node server

and you can test it with:

curl http://localhost:3000

Request/Response validation and hooks

Of course, Fastify can do much more than this.

For example, you can easily provide input and output validation using JSON Schema and perform specific operations before the handler is executed:

async/await const fastify = require('fastify')({ logger: true }) fastify.route({ method: 'GET', url: '/', schema: { // request needs to have a querystring with a `name` parameter querystring: { name: { type: 'string' } }, // the response needs to be an object with an `hello` property of type 'string' response: { 200: { type: 'object', properties: { hello: { type: 'string' } } } } }, // this function is executed for every request before the handler is executed preHandler: (request, reply, done) => { // E.g. check authentication done() }, handler: (request, reply) => { reply.send({ hello: 'world' }) } }) fastify.listen(3000, (err) => { if (err) { fastify.log.error(err) process.exit(1) } fastify.log.info(`server listening on ${fastify.server.address().port}`) }) const fastify = require('fastify')({ logger: true }) fastify.route({ method: 'GET', url: '/', schema: { // request needs to have a querystring with a `name` parameter querystring: { name: { type: 'string' } }, // the response needs to be an object with an `hello` property of type 'string' response: { 200: { type: 'object', properties: { hello: { type: 'string' } } } } }, // this function is executed for every request before the handler is executed preHandler: async (request, reply) => { // E.g. check authentication }, handler: async (request, reply) => { return { hello: 'world' } } }) const start = async () => { try { await fastify.listen(3000) fastify.log.info(`server listening on ${fastify.server.address().port}`) } catch (err) { fastify.log.error(err) process.exit(1) } } start()

TypeScript Support

Fastify is shipped with a typings file, but you may need to install @types/node , depending on the Node.js version you are using.

The following example creates a http server.

We pass the relevant typings for our http version used.

By passing types we get correctly typed access to the underlying http objects in routes.

If using http2 we'd pass <http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse> .

For https pass http2.Http2SecureServer or http.SecureServer instead of Server.

This ensures within the server handler we also get http.ServerResponse with correct typings on reply.res .

async/await import * as fastify from 'fastify' import { Server, IncomingMessage, ServerResponse } from 'http' const server: fastify.FastifyInstance = fastify({}) const opts: fastify.RouteShorthandOptions = { schema: { response: { 200: { type: 'object', properties: { pong: { type: 'string' } } } } } } server.get('/ping', opts, (request, reply) => { reply.send({ pong: 'it worked!' }) }) server.listen(3000, (err) => { if (err) { server.log.error(err) process.exit(1) } server.log.info(`server listening on ${server.server.address().port}`) }) import * as fastify from 'fastify' import { Server, IncomingMessage, ServerResponse } from 'http' const server: fastify.FastifyInstance = fastify({}) const opts: fastify.RouteShorthandOptions = { schema: { response: { 200: { type: 'object', properties: { pong: { type: 'string' } } } } } } server.get('/ping', opts, async (request, reply) => { return { pong: 'it worked!' } }) server.listen(3000, (err) => { if (err) { server.log.error(err) process.exit(1) } server.log.info(`server listening on ${server.server.address().port}`) })

Visit the Documentation to learn more about all the features that Fastify has to offer.