DEV Community

Jastria Rahmat
Jastria Rahmat

Posted on • Updated on

Serverless REST API using Nodejs & Express

prerequisites and acknowledgements

Before implementing, make sure these things are prepared:

  • AWS keys e.g:
serverless config credentials --provider aws --key XXXXXXXXXXXXXX --secret XXXXXXXXXXXXXXXXXXXX
Enter fullscreen mode Exit fullscreen mode

please refer to this page.

  • If you're using different provider other than AWS, there may be different configurations. So far, I've only tested on AWS Lambda.
  • I use nodejs 14.x
  • My own environment is Linux Mint. I use both 19.3 and 20.02 as my home and work computer.

  • if you have a problem on installing npm globally, use:

wget -O- https://raw.githubusercontent.com/ijash/npm-g_nosudo/master/npm-g-nosudo.sh | sh

Enter fullscreen mode Exit fullscreen mode

It's a script to install global (-g) npm packages without using sudo.

  • I'm not an expert, but this method works for me.

Introduction

Why I use serverless? Aside from my workplace requirement, I found that serverless quite interesting. It scale automatically, so we don't have to worry about load balancing.

Many of my colleagues ask the meaning of serverless. To put it together, serverless is not without server at all, it's simply a platform without the needs of configuring the server such as installing OS, managing Ports, managing the startup scripts, etc..

Installation

To use serverless, open terminal and create a new project. Say, we name it serverless-express

mkdir serverless-express
Enter fullscreen mode Exit fullscreen mode

then move to that directory.

cd serverless-express
Enter fullscreen mode Exit fullscreen mode

init the project

npm init -y
Enter fullscreen mode Exit fullscreen mode

Install serverless alongside express.

npm i express serverless-http
Enter fullscreen mode Exit fullscreen mode

Setup Express

Create index.js.

touch index.js
Enter fullscreen mode Exit fullscreen mode

And fill it with these codes using your favorite code editor.

const serverless = require('serverless-http');
const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.send('Hello World!')
})

module.exports.server = serverless(app);
Enter fullscreen mode Exit fullscreen mode

The serverless-http package is a middleware that act as a bridge for your express app. So, all you need is use serverless function and pass your main express app to it.
And remember the name module.exports.server, because, the server object will be used in serverless.

Setup Serverless YAML file

create serverless.yml

touch serverless.yml
Enter fullscreen mode Exit fullscreen mode

and fill it with this configuration.

service: serverless-express

provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: us-east-1

functions:
  app:
    handler: index.server
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
Enter fullscreen mode Exit fullscreen mode

Pay an extra attention to indentation. Just like python language, YAML uses indentation to differentiate the scope.

You can name service tag as you like, It's like a project name.
In scope provider you need to fill it to fit your platform.
functions scope tells you how the endpoints behave.
All about the configuration guides are available here.

Deploying

After all set, It's time to deploy.
If required, use:

sls login
Enter fullscreen mode Exit fullscreen mode

Or, directly use:

serverless deploy
Enter fullscreen mode Exit fullscreen mode

That's it. you will get the deployed URL in the terminal output.
And It's a good practice if you use serverless dashboard to monitor your activity.

Top comments (0)