Skip to content
{Ed}uardo Veras edited this page Jul 7, 2022 · 2 revisions

Build Status: Linux Libraries.io dependency status for latest release npm node-current GitHub code size in bytes

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.

Install

$ npm install serverless-aws-router

Creating a Router Handler

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:

serverless.yml

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.

Server({ options })

'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.

Clone this wiki locally