-
Notifications
You must be signed in to change notification settings - Fork 1
Home
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.
$ 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: nodejs12.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.