Serverless REST API using Nodejs & Express
Published: 2021-08-11 14:42:50

# prerequisites and acknowledgements
Before implementing, make sure these things are prepared:
- [AWS](https://aws.amazon.com/lambda/) keys e.g:
```sh
serverless config credentials --provider aws --key XXXXXXXXXXXXXX --secret XXXXXXXXXXXXXXXXXXXX
```
please refer to [this](https://www.serverless.com/framework/docs/providers/aws/guide/credentials/) 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:
```sh
wget -O- https://raw.githubusercontent.com/ijash/npm-g_nosudo/master/npm-g-nosudo.sh | sh

```
It's a [script](https://github.com/ijash/npm-g_nosudo) 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](https://dev.to/devgraph/learn-all-about-load-balancing-technologies-3h9n).

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`
```sh
mkdir serverless-express
```
then move to that directory.
```sh
cd serverless-express
```
init the project
```
npm init -y
```
Install serverless alongside express.
```sh
npm i express serverless-http
```
# Setup Express

Create `index.js`.
```sh
touch index.js
```
And fill it with these codes using your favorite code editor.
```js
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);
```
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`
```sh
touch serverless.yml
```
and fill it with this configuration.

```yml
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+}'
```
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](https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#serverlessyml-reference/).

# Deploying
After all set, It's time to deploy.
If required, use:
```sh
sls login
```
Or, directly use:
```sh
serverless deploy
```
That's it. you will get the deployed `URL` in the terminal output.
And It's a good practice if you use [serverless dashboard](https://www.serverless.com/dashboard/) to monitor your activity.