Optimized web framework for serverless environment
To prevent the cold-start of serverless functions from getting too long, a good alternative is not to use Express as it simulates the creation of an entire server and configures all routes and all middlewares of each of the routes, with only 1 being used.
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 0.10 or higher is required.
If this is a brand new project, make sure to create a package.json first with
the npm init command.
Installation is done using the
npm install command:
$ npm install --save serverless-api-manager// index.tsimporttype{APIGatewayEvent}from'aws-lambda';importAnyLoggerLibraryfrom'any-logger-library';import{ServerlessApiManager,Services}from'serverless-api-manager';import{MyAction}from'./actions/my-action';import{IContext}from'./interfaces/IContext';exportconsthandler=async(event: APIGatewayEvent)=>{constmanager=newServerlessApiManager<APIGatewayEvent,IContext>().withEvent(event).withService(Services.API_GATEWAY).withContextId('13245-12345-13245-12345').withLogger(newAnyLoggerLibrary()).withContext({appName: 'my-example'}).withAction(newMyAction());returnmanager.run();};// interfaces/IContext.tsexportinterfaceIContext{appName: string;}// actions/my-action.tsimport{IContext}from'../interfaces/IContext';exportclassMyActionimplementsIAction<IContext>{execute: ExecutorHandler<IContext>=(request,response)=>{const{ body, context, headers, params, query }=request;context.logger.debug(`Initializing my action: ${context.appName}`);// ALL YOUR CODE GOES HEREcontext.logger.debug('Sending my response');returnresponse.headers({ ...headers,'content-type': 'application/json'}).status(200).json({foo: 'bar', body, params, query });};}