Monday, January 2, 2023

 

Exposing a web application as serverless:

The right tool for a job makes it easy. Serverless-http is a library registered with the NPM library manager that can be used to configure applications for hosting on serverless infrastructure such as AWS Lambda.

This makes it easy to wrap existing apis and applications and export it with an entry point for a serverless event listener.

Sample of using this library is as simple as follows:

import { Router } from '../router'

import serverless from 'serverless-http'

export const run = serverless(Router)

where the Router is an object that defines all the routes and can be instantiated from a server with a runtime such as Koa as shown above or Express as shown below.

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

const express = require('express');

const app = express();

app.use(/* register your middleware as normal */);

const handler = serverless(app, { provider: 'azure' });

module.exports.funcName = async (context, req) => {

  context.res = await handler(context, req);

}

No comments:

Post a Comment