1.3.1 • Published 2 years ago

serverless-aws-router v1.3.1

Weekly downloads
7
License
Apache-2.0
Repository
github
Last release
2 years ago

serverless-aws-router

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.

Documentation and options

All configuration options, methods, and events can be found on the Wiki section on this repo.

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

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.

Examples

You can find some usage examples on the test folder on this repo.

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.11

3 years ago

1.0.12

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago