Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 1.64 KB

Usage.md

File metadata and controls

69 lines (50 loc) · 1.64 KB

Usage

Unless your application requires complex routing, route handlers can be defined within the Lambda function scope. Otherwise route handlers are loaded from appName/src/routes directory in hierarchical order, starting with the default handler appName/src/app.js as described below.

Synchronous example

// .. appName/src/app.js

'use strict';

// Load module.
const Router = require('@lambda-lambda-lambda/router');

/**
 * @see AWS::Serverless::Function
 */
exports.handler = (event, context, callback) => {
  const {request, response} = event.Records[0].cf;

  const router = new Router(request, response);
  router.setPrefix('/api'); // optional, default /

  // Globally scoped to /api/*
  router.use(function(req, res, next) {
    if (req.method() === 'CONNECT') {
      res.status(405).send();
    } else {
      next(); // Run subsequent handler
    }
  });

  // Send root response.
  router.get('/', function(req, res) {
    res.status(501).send();
  });

  // .. everything else.
  router.default(function(req, res) {
    res.status(404).send();
  });

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

Asynchronous example

// .. appName/src/app.js

'use strict';

// Load module.
const Router = require('@lambda-lambda-lambda/router');

/**
 * @see AWS::Serverless::Function
 */
exports.handler = async (event) => {
  const {request, response} = event.Records[0].cf;

  const router = new Router(request, response);

    // .. Router Methods

  return await router.response();
};