Fastify is a quick and simple alternative to Express. It’s the fastest NodeJS framework, and we find it great for writing apps timely.
Unlike Express, Fastify ships with builtin error handling; that means there’s no need to wrap every route in a try-catch block! Besides, it doesn’t require external modules to work with JSON. Consider the following two blocks:
Though they accomplish the same task, the second one is less bloated and easier to read.
Serverless is an architecture for deploying code without maintaining a server. It only runs as needed, and the cloud provider handles most “ops” related tasks.
AWS’s serverless cloud service is called Lambda, and we tend to reach for it quite a bit. Lambda is not inherently HTTP based; it exposes its own interface for invoking its “functions” (blocks of serverless code). So we use a library called serverless-http to adapt our functions into REST APIs, which any clients may consume.
Finally, the wonderful Serverless Framework handles the nitty-gritty of launching our code. It orchestrates ALL the cloud services in play; S3 for pushing code, API Gateway for handling HTTP requests, and of course Lambdas. But you don’t have to worry about any of that- the tool takes care of these things behind the scenes.
Fastify + Serverless
From start to finish, we can launch an app in these steps:
1.) Create Project
To keep it simple, we created a basic Fastify project that you can download here. All you need to do is clone the repo, and run the “sls deploy” command from the root folder.
Finally, you may replace the logic in the main file with your own app and deploy magically.