DEV Community

Cover image for Build and Deploy a Rest API  Using Serverless, Express, and Nodejs
Sai gowtham
Sai gowtham

Posted on • Updated on

Build and Deploy a Rest API Using Serverless, Express, and Nodejs

Today I'm showing how to Build and deploy an express/Node Rest API using serverless lambda.

We know Serverless is growing so fastly and a lot of companies are saving money by using Serverless computing.

Amazon web services offer us wide variety of services to build and deploy Small-scale to Large-Scale Applications.

What is Aws Lambda?

AWS Lambda runs your code without provisioning or managing servers it means you don't need to buy or manage a server. You only pay whenever your code runs or whenever a user visits your site.

lamda

What are Requirements?

I'm using the serverless framework Cli to create and deploy apps.

Open your terminal

npm install -g serverless

sls login // SLS is a shortcut of serverless

Enter fullscreen mode Exit fullscreen mode

After sls login, You need to configure Your Aws Credentials with a serverless framework.

Get Aws Credentials

Once It is done Your are good to go.

Creating and Deploying the Rest API endpoints

Create a Template in Your Working Directory

serverless create --template aws-nodejs
Enter fullscreen mode Exit fullscreen mode

The above command generates the boilerplate.

Now we need to initialize the Package.json file and install
some dependencies.

npm init -y // generates package.json file

npm i -s body-parser cors express mongoose serverless-http
Enter fullscreen mode Exit fullscreen mode

Open Your app folder in your Favourite code editor.

Create an app.js file and import the requirements for the database i'm using the mongodb.

We need to create a Model for the MongoDB database.

create a data.js file.

const mongoose = require('mongoose');

const Users = mongoose.Schema({
    name: String,
    age: Number,
})

const User = mongoose.model('User', Users);

module.exports = User;
Enter fullscreen mode Exit fullscreen mode

This is our simple User model.

Open your serveless.yml and add the below code.

  • runtime nodejs8.10 why because Aws lambda currently supports v8.10.
  • handler: we need to define the app.handler it means please look into app.js file.
  • serverless-offline plugin helps us to run your app into the local environment.

Open your Terminal and run.

sls offline start // to run the code in local environment
Enter fullscreen mode Exit fullscreen mode

offline

Wow, our code is running without any errors.

How to deploy the Code into Aws Lambda?

  • It is very easy serverless framework does all things in the background

Open your terminal and run.

sls deploy 
Enter fullscreen mode Exit fullscreen mode

Once you run above command after some time your API endpoints are visible in your terminal.

deploy

How to Test the API Endpoints?

  • I'm using the Postman to test the API endpoints.

Post Method

postmethod

Get Method

get

How to deploy your existing rest API?

  1. Open your app.js file or main.js file and add these export.
const serverless = require('serverless-http');
const express = require('express');
const app = express();

*express code*

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

2.Setup your serverless.yml

How to Monitor and Track your Lamda Invocations?

For monitoring, Debugging and error detection of lambdas we are using Dashbird

Why Dashbird?

  • Dashbird helps us actively monitoring the health and errors.

  • One main thing about Dashbird is its user-friendly Interface.

  • Dashbird visualizes all your AWS Lambda metrics like
    memory utilization, invocation count, and execution duration.

DashBird Interface

Dashbird monitoring

Hope you guys enjoyed if you have any doubts feel free to ask.

Other Interesting Posts on Serverless

How to Build Your First Serverless Website

Top comments (4)

Collapse
 
magnusriga profile image
Magnus

Sai, great guide!

Question: From your examples it looks like serverless (Lambda, Functions, or other) just sits right before the express layer and routes/passes all incoming HTTP traffic directly on to express. Given that express was handling the routes just fine by itself, before we introduced serverless, what's the purpose of it? Why do we want serverless in there, just passing requests on?

Thank you

Collapse
 
pbeltranes profile image
Paul Beltran

Hi Sai, very great guide!

Only I have a warning use express and serverless to the same time, went someone create a service using aws like 50 endpoints, AWS put this problem: serverless.com/blog/serverless-wor...

So you have to change one framework or change to microservices.

Collapse
 
sm00g15 profile image
Sm00g15

pretty awesome! How can we go about getting our API's to reflect one of our subdomains?

Collapse
 
hjrobinson profile image
hjrobinson • Edited

I can't get the post and get requests to work when using an Atlas connection string. I keep getting 502 Bad Gateway with an internal server error.