Building a Fastify NodeJS server

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I recently needed a super simple webserver to listen to POST requests.
Before we used express to host a simple Node server.
However, I kept hearing amazing things about Fastify, and we are actually using this at daily.dev.
So time to try it out for a project and see what it exactly can do for us.
Fastify can be used to quickly spool up a server in Node.js, much like express or its alternatives. Fastify thrives on being fast, as the name suggests. They also state that it can handle large requests while keeping overhead very low.
Say no more. Let's try it out.
In this article, I want to create a super simple Node.js server. We actually need a POST route for the application I have in mind, but let's set up a GET and a POST route to see how easy this is.
First, we'll start by creating a new application and setting its structure.
mkdir fastify-server && cd fastify-server
Now that we have the folder, we can initialize it as a node application.
npm init
And the last step here is to install Fastify.
npm i fastify
Now it's time to create the server file. Create a file called server.js in your project.
The most basic setup to get Fastify up and running is as follows.
// 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);
When you now run 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.

Wow, 5 lines of code and our server is set up already. This is looking pretty great.
But let's make the start of the server a bit more robust by using the code Fastify recommends to us.
// Start the server
const start = async () => {
try {
await fastify.listen(3000);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();
This does the same thing, but we now catch any error that might occur and stop the server in that case.
We can also quickly add named routes if we want to like this:
// Declare a named route
fastify.get('/chris', async (request, reply) => {
return {hello: 'chris'};
});

However, often you might want a dynamic route. We want to get the name, but this parameter is dynamic based on the URL.
// Declare a dynamic route
fastify.get('/name/:name', async (request, reply) => {
return {hello: request.params.name};
});
This will be a route that we can access like so: /name/jason.
The name can be dynamic, rendering the name in the output.

Now that we have this in place let's see how we can make a post-only route to post some data.
For now, it would be great if the route only listens to the POST option and dumps whatever we put in.
// Declare a post route
fastify.post('/dump', async (request, reply) => {
return request.body;
});
If we now try to POST to this route using a API Client like Insomnia, we can see it works!

Fastify is really great to quickly spool up a Node server, and from these initial tests, it's quick and easy to use.
I'll definitely be using more of it in the future. If you would like to have a play with the creation from today, I've hosted it on GitHub for you.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter