Deploying a Serverless Express REST API in 10 minutes

If you are a Node.js developer or you’ve built an API with Node.js, there’s a good chance you have used Express.js. Express is one of the most popular Node.js frameworks. It provides lightweight, powerful tools for HTTP servers. It’s a great framework for single-page apps, websites, hybrids, or public HTTP APIs.
For example, the simplest Express app looks like the following code snippet:

Run the node app.js command and visit http://localhost:3000/user in your browser, and you’ll see a list of users. Wow! You have a working Express app. Now’s the hard part — How can we make it available for everyone? How we manage if thousand of users started using it? On the other hand, do you still need to pay for hours if no one is using your API?

Think Serverless

Serverless Architecture is the best way to answer all of the above questions. Serverless Architecture lets you execute a piece of code and only charges you for the resources you use. As a developer, this means that you don’t have to think about managing servers and scaling. You just focus on code.

In this article, we will talk about serverless on AWS. Most popularly, it consists of AWS Lambda and API Gateway. But how do we use them? Fear not, here we will explore how we can use them to get Serverless benefits and Express features together.

If you still don’t have experience in AWS serverless environment. Don’t worry; we are going to use SLAppForge Sigma, a cloud-based hybrid IDE providing a fairly simple, intuitive environment, with easy, drag-and-drop composition combined with the full power of written code. If you haven’t used Sigma before, no need to worry. Just signup for a free account and create a new Sigma project by following the few simple steps mentioned in this guide.

Our first step is to create a Lambda function. The nice thing is, if you created a project, Sigma automatically creates a Lambda function named same as the project for you, with the following start-up code:

Now you need to create an Express app inside this Lambda function. So please copy and paste previously created Express code from above.

Convert the express app to make it ready to deploy on Lambda environment

For that, we need to remove the Express app app.listen line and add the serverless-http module. This module is then given the whole Express app object and exported with a handler.

Now we need to connect this with the Lambda handler which is already implemented. Here is the complete code:

Then we need to define an API Gateway endpoint to accept requests and set it as the trigger for our lambda function.

For that, drag an API Gateway resource from the resource panel on the left and drop it on top of the red-colored event variable of the lambda function.



Then on the configuration panel, fill the APIGateway details as follows:



Note: You should enable Lambda Proxy Integration under Advanced configurations.

Once you are done, click on Inject button to add the trigger. Now the event variable should have been turned into green. You need to create another trigger to handle user/* requests. So add another trigger as follows;



Then let’s install all npm dependencies using the Add Dependency button in the top left corner of the toolbar, as



Now we have completed the development of our Serverless Express App, it’s time to save it and deploy it. So click on the Deploy button at the toolbar and it will first prompt you to commit the project to your GitHub repository. Once it is committed successfully the build process will execute automatically, followed by the deployment process.
Once the deployment is done, you will see a deployment summary similar to the following.



Make a note of URL endpoints and then use a HTTP client to make a GET request. If all goes well, you will receive a response of the following format

"id": 1,
"name": "sudarshana"
"id": 2,
"name": "lakmal"

Great! You have successfully deployed your Express app as a pay-per-request and highly scalable serverless application.