Create a Serverless API with Fastify Framework
Serverless API is an API which is running your core application without a http server. So there is no http server in your API application. Everything is run by event driven in server machine. Serverless are most used in cloud systems server.
Actualy there are more benefits to use serverless, but these three points is very useful benefit to use serverless for your project.
Serverless is more cost-effective because you only pay as you go. You don’t need to buy fixed cost for the machine server.
Handled by Cloud
There is no server in your API application, so everything is handled by cloud vendors.
Because serverless API is run by event driven, you are able to invoked your function directly without a http server if you are in same microservices environment. This is similar with FaaS (Function as a Service).
Actualy there is more const of using serverless, but I only describe 3 points which is very important to consider.
Serverless is using resource directly with OS of the server machine. Means you are on limit when compute for high task.
It is better for you to keep your serverless application always small. But creating a small application doesn’t mean that you can’t scale your serverless application. If you in microservices environment, of course you are able to scale it.
Serverless could suffer from response latency. Because serverless will start runtime from the begin for every process so this will always add more latency.
You can not control the security of serverless. Because serverless is using resources directly from OS. When the OS having vulnerable, it could affect to your serverless application.
In this article I will use Fastify framework to create a serverless API. Why?
Fastest NodeJS Framework
Fastify is the fastest NodeJS Framework today. Fastify still on the top ranking of performance speed or benchmark speed than other well known nodejs framework.
Everything is Plugin
Fastify using modular architecture concept. So this will easy and fast to develop, because we can work together in parallel.
Simple, Flexible and Minimalist
Fastify is very simple and easy to learn even you are newbie in NodeJS. Because of minimalist and their simplicity, I feel flexible to choose any library in
So the tutorial to create serverless with Fastify is :
To install fastify is just run this command below
$ npm install fastify
If installation process done, you can go to the next step below.
serverless.js file in new directory
index.js file in same directory
You have to create
now.js file on root of your project. Just copy and paste this code below.
Done, this is very simple right? To add more advanced routes, just see Fastify documentation at here.
You can not test your serveless API with HTTP request right now because you need a serveless runtime container service. So the only way to test is, you have to create your own unit test for example with mocha.
To deploy your first serverless API after following the tutorial above, now follow this steps below:
Create GitHub Account
You have to create a Github account at here. If you already have an account in GitHub, just upload your project into your new repository.
Create Zeit Now Account.
You have to create a Zeit Now account at here. If you already have an account in Zeit Now, you can just create new app in Zeit Now, then follow the steps on Zeit Now. It is very easy, no hard configuration and no code required.
Done, your serverless API is will deployed in minutes. No domain required, because Zeit Now will give you free. But you are able to change with your custom domain later.
Serverless is event driven concept. So the main point to build a serverless API is you have to create event driven application then create a http route for communication with client request.
Fastify framework wasn’t designed for serverless framework, but Fastify has a way for being serverless API. But to run this serverless API you need a hosting provider who provide serverless container service for example Zeit Now. Why I choose
Zeit Now? Because they very simple to use or to deploy, they have a zero configuration for nodejs and they have a free service. I use they free service to make research before I write this tutorial.
Don’t forget, free service is always have it’s limitation, if you are serious about your business, I recommend you to buy the premium hosting service from them.
Thank you for your time to reading my article.