# Launching a Serverless, Fastify App in Five Minutes

Github Repo with template here (link)

Fastify

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:

With Express:

const drawSignatures = require('./drawSignatures')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.post('/', async (request, response) => {
  try {
    const bytes = await drawSignatures.drawSignatures(
      request.body.doc,
      request.body.signature
    )
    return response.send(bytes)
  } catch (err) {
    return response.status(500).send({ message: err.message })
  }
})
module.exports = app

With Fastify:

const drawSignatures = require('./drawSignatures')
const fastifyImp = require('fastify')
const fastify = fastifyImp({
  logger: true,
})
fastify.post('/', async (request, reply) => {
  // Some sort of request validation
  const bytes = await drawSignatures.drawSignatures(
    request.body.doc,
    request.body.signature
  )
  reply.send(bytes)
})

module.exports = fastify

Though they accomplish the same task, the second one is less bloated and easier to read.

Serverless

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:

0.) Install Serverless and setup AWS credentials (pre-req)

1.) Create Project

2.) Deploy

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.