Router

Provides HTTP request/response handling.

Constructor

new Router(request, response)

Parameters:
NameTypeDescription
requestCloudFrontRequest

CloudFront request object.

responseCloudFrontResponse | undefined

CloudFront response object (optional).

Example
exports.handler = (event, context, callback) => {
  const {request, response} = event.Records[0].cf;

  const router = new Router(request, response);

  callback(null, router.response());
};

  ..

exports.handler = async (event) => {
  const {request, response} = event.Records[0].cf;

  const router = new Router(request, response);

  return await router.response();
};

Methods

default(route)

Set router fallback (default route).

Parameters:
NameTypeDescription
routefunction

Route function.

Example
router.default(function(req, res) {
  res.status(404).send();
});

delete(path, route)

Handle HTTP DELETE requests.

Parameters:
NameTypeDescription
pathString

Request URI.

routefunction

Route function.

Example
router.delete('/api/test', function(req, res) {
  res.status(200).send();
});

get(path, route)

Handle HTTP GET requests.

Parameters:
NameTypeDescription
pathString

Request URI.

routefunction

Route function.

Example
router.get('/api/test', function(req, res) {
  res.setHeader('Content-Type', 'text/html');
  res.status(200).send('Hello World');
});

handle(path, func)

Handle the Route/Middleware request (add to stack).

Parameters:
NameTypeDescription
pathString

Request URI.

funcfunction

Route/Middleware function.

patch(path, route)

Handle HTTP PATCH requests.

Parameters:
NameTypeDescription
pathString

Request URI.

routefunction

Route function.

Example
router.patch('/api/test', function(req, res) {
  res.status(204).send();
});

post(path, route)

Handle HTTP POST requests.

Parameters:
NameTypeDescription
pathString

Request URI.

routefunction

Route function.

Example
router.post('/api/test', function(req, res) {
  res.status(201).send();
});

put(path, route)

Handle HTTP PUT requests.

Parameters:
NameTypeDescription
pathString

Request URI.

routefunction

Route function.

Example
router.put('/api/test', function(req, res) {
  res.status(201).send();
});

response() → {CloudFrontResponse|Promise.<CloudFrontResponse>}

Return CloudFront response object.

Returns:
Type: 
CloudFrontResponse | Promise.<CloudFrontResponse>

setPrefix(value)

Set URI path prefix.

Parameters:
NameTypeDescription
valueString

Request URI.

Example
router.setPrefix('/api');

use(arg, func)

Load the Route (e.g. Middleware) handler.

Parameters:
NameTypeDescription
argfunction | String

Route/Middleware or Request URI.

funcfunction

Route/Middleware function (optional).

Example
// Include function as middleware.
const Middleware = require('./path/to/middleware');

router.use(Middleware);

  ..


// Run function for every request.
router.use(function(req, res, next) {
  if (req.method() === 'POST') {
    res.status(405).send();
  } else {
    next();
  }
});

  ..

// Run function on URI path only.
router.use('/api/test', function(req, res) {
  res.setHeader('Content-Type', 'text/html');
  res.status(200).send('Hello World');
});