Skip to content

1. Server Options

Eduardo Veras edited this page Jul 7, 2022 · 1 revision

The server options control the behavior of the server object.

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

options.Joi

object | mandatory

To avoid versioning problems between the Joi library, add the Joi package globally in your project, and pass it when you are creating a new server.

options.validationAllowUnknown

deprecated boolean | optional | default: false (v1.0.10+)

When true, allows Joi validation object to contain unknown keys which are ignored.

Note: This option was deprecated, please use joiOptions.allowUnknown instead.

options.joiOptions

object | optional | default: undefined (v1.0.13+)

Overwrite Joi default options, please see Joi documentation for all possible options.

options.wrapResponse

boolean | optional | default: true (v1.0.7+)

Used to indicate if all response data should be wrapped before the final response.

If set to false the reply will always act as reply.raw().response(content).

options.parseQueryString

boolean | optional | default: false (v1.1.0+)

If set to true the system will auto try to parse first the query string values using JSON.parse() and then parse the fields using qs.parse().

When is false:

// GET /endpoint?foo[bar]=baz
request.query = {
	"foo[bar]": "baz"
};

When is true:

// GET /endpoint?foo[bar]=baz
request.query = {
	"foo": {
		"bar": "baz"
	}
};
// GET /endpoint?foo[bar]=1&foo[baz]=2
request.query = {
	"foo": {
		"bar": 1,
		"baz": 2
	}
};
// GET /endpoint?foo[bar][baz]=foobarbaz
request.query = {
	"foo": {
		"bar": {
			"baz": "foobarbaz"
		}
	}
};
// GET /endpoint?foo={"bar": "baz"}
request.query = {
	"foo": {
		"bar": "baz"
	}
};
// GET /endpoint?foo[bar]={"baz": "foobarbaz"}
request.query = {
	"foo": {
		"bar": {
			"baz": "foobarbaz"
		}
	}
};

options.auth

object | optional | default: { method : "jwt", function : null }

Sets the default method to be used to authenticate requests.

options.auth.method

string | mandatory | default: "jwt"

Define the authorization method, which can be:

  • jwt - by default the server will use JWT to authenticate and validate requests.
  • custom - sets authorization to use a custom function to authenticate requests. This method will only work together with the function option.

options.auth.function

async function(event, route, request) | optional | default: null

This option will only work when the method is set to custom, and the function must return:

  • null - if the authorization fails.
  • object - an object with the will be saved (shallowly copied) into request.auth.credentials and will set request.auth.isAuthenticated to true.