Hello guys, today I will write a tutorial about create serverless API with Fastify framework. Serverless means you don’t need a NodeJS server to run. Are you sure? NodeJS is a platform to run JavaScript on the server side, but how we can make it serverless? If you want to know more detail? Just check this out.

What is Serverless API ?

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.

Benefit Serverless

Actualy there are more benefits to use serverless, but these three points is very useful benefit to use serverless for your project.

  1. Eficient Cost
    Serverless is more cost-effective because you only pay as you go. You don’t need to buy fixed cost for the machine server.

  2. Handled by Cloud
    There is no server in your API application, so everything is handled by cloud vendors.

  3. Event Triggered
    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).

Const of Serverless

Actualy there is more const of using serverless, but I only describe 3 points which is very important to consider.

  1. Resource limit
    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.

  2. Performance
    Serverless could suffer from response latency. Because serverless will start runtime from the begin for every process so this will always add more latency.

  3. Security
    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.

Create Serverless with Fastify

In this article I will use Fastify framework to create a serverless API. Why?

  1. 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.

  2. Everything is Plugin
    Fastify using modular architecture concept. So this will easy and fast to develop, because we can work together in parallel.

  3. 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 NPMJS.

So the tutorial to create serverless with Fastify is :

1. Install Fastify

To install fastify is just run this command below

1
$ npm install fastify

If installation process done, you can go to the next step below.

2. Create serverless.js

Now create serverless.js file in new directory api/

1
2
3
4
5
6
7
8
9
10
'use strict'

const build = require('./index')

const app = build()

module.exports = async function (req, res) {
await app.ready()
app.server.emit('request', req, res)
}

3. Create index.js

Now create index.js file in same directory api/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
'use strict'

const fastify = require('fastify')

function build () {
const app = fastify({
logger: true
})

app.get('/', async (req, res) => {
return {statusCode:res.statusCode,message:'Welcome to my serverless API.'}
})

return app
}

module.exports = build

4. Create now.js

You have to create now.js file on root of your project. Just copy and paste this code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"version": 2,
"builds": [
{
"src": "api/serverless.js",
"use": "@now/node",
"config": {
"helpers": false
}
}
],
"routes": [
{ "src": "/.*", "dest": "/api/serverless.js"}
]
}

Done, this is very simple right? To add more advanced routes, just see Fastify documentation at here.

How to test ?

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.

How to Deploy ?

To deploy your first serverless API after following the tutorial above, now follow this steps below:

  1. 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.

  2. 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.

How this Works

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.

Conclusion

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.

Actualy I don’t have any affiliate with Zeit Now, Of course you can make serverless API with other hosting provider like Google Cloud Run, AWS Lambda, etc.

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.