Minimal explanation, maximum focus on coding. Follow our step-by-step blog for a ready-to-deploy serverless API.

Serverless Node.js

Install Serverless globally:

npm install -g serverless

Create a new Serverless service:

serverless create --template aws-nodejs --path myService
cd myService

Install Express and Serverless HTTP:

npm init -y
npm install express serverless-http

Create a new file app.js:

const express = require('express');
const app = express();

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

module.exports = app;

Modify handler.js:

const serverless = require('serverless-http');
const app = require('./app.js');

module.exports.server = serverless(app);

Modify serverless.yml:

service: myService

provider:
  name: aws
  runtime: nodejs12.x

functions:
  app:
    handler: handler.server
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

Deploy your service:

serverless deploy

This will deploy your Express application as a Serverless service. You can now access your application via the endpoint provided by AWS.

Read more blogs http://information-24.com

Categorized in: