This project aims to be a lightweight router system for Serverless framework for AWS Lambda. The code design was inspired on Hapi framework, and try to mimic the routes, validations, requests and response models of traditional NodeJS router frameworks.
All configuration options, methods, and events can be found on the Wiki section on this repo.
$ npm install serverless-aws-router
The router handler needs two parts: a serverless.yml
that will have the main route configuration, and a server.js
that will have the inner endpoints. A very basic example will look like the following:
service: serverless-social-network
provider:
name: aws
runtime: nodejs16.x
functions:
app:
handler: server.handler
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{proxy+}
method: ANY
cors: true
All HTTP endpoints paths and methods, are pointed to the server.handler
, which will call the exported method handler
at the server.js
file.
'use strict';
const slsRouter = require('serverless-aws-router');
const Joi = require('joi');
const server = new slsRouter.Server({
Joi, //Required
joiOptions : { //Optional
abortEarly: true, //Optional
allowUnknown: false, //Optional
cache: true //Optional
},
wrapResponse: true, //Optional, default true
auth : { //Optional
method: 'jwt', //Default 'jwt'
function: null //Default null
},
parseQueryString: false //Optional, default false (v1.1.0+)
});
server.route({
method: 'GET',
path: '/',
handler: async (request, reply) => {
return reply.response({ Hello : "World!" });
}
});
module.exports.handler = (event, context, callback) => server.handler(event, context, callback);
First create a new slsRouter.Server()
, then add your routes to the server, and finally export the server.handler()
that will receive the serverless requests.
You can find some usage examples on the test folder on this repo.