Skip to content

Latest commit

History

History
56 lines (39 loc) · 1.65 KB

File metadata and controls

56 lines (39 loc) · 1.65 KB

Middleware

Synchronous example

// .. appName/src/middleware/ContentTypeHeader.js'use strict';/** * Middleware to send Content-Type header. */module.exports=(req,res,next)=>{res.setHeader('Content-Type','text/html');next();// Run subsequent handler.};

Asynchronous example

// .. appName/src/middleware/SessionCheck.js'use strict';const{RouterError}=require('@lambda-lambda-lambda/router/src/router/Error.js');/** * Middleware to define session state, if exists. */module.exports=async(req,res,next)=>{if(awaitcheckSession()){// Passed down the Router stack.req.plugin('session',true);}else{// Write to CloudWatch, continue..thrownewError('Output to console');}// next() should be omitted};

See L³ middleware for more complex use cases.

Handling exceptions

Exception typeDescription
throw new RouterErrorWhen called will immediately exit the middleware stack and write the exception to CloudWatch logs.
throw new ErrorWhen called will write the exception to CloudWatch and run the subsequent handler in middleware stack.
Promise.reject()Same as throw new Error with no exception thrown.