43
loading...
This website collects cookies to deliver better user experience
GET
and a POST
route to see how easy this is.mkdir fastify-server && cd fastify-server
npm init
npm i fastify
server.js
in your project.// Require the framework and instantiate it
const fastify = require('fastify')({logger: true});
// Declare a route
fastify.get('/', async (request, reply) => {
return {hello: 'world'};
});
// Start the server
fastify.listen(3000);
node server
, it will spool up the server on port 3000
, and by visiting this in your browser, you should see the output we defined.// Start the server
const start = async () => {
try {
await fastify.listen(3000);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();
// Declare a named route
fastify.get('/chris', async (request, reply) => {
return {hello: 'chris'};
});
// Declare a dynamic route
fastify.get('/name/:name', async (request, reply) => {
return {hello: request.params.name};
});
/name/jason
.POST
option and dumps whatever we put in.// Declare a post route
fastify.post('/dump', async (request, reply) => {
return request.body;
});
POST
to this route using a API Client like Insomnia, we can see it works!