request-received
Route middleware for Koa and Express that adds a request received high-resolution timer and Date to the request object using easily accessible Symbols to prevent request object pollution. Made for Cabin.
Table of Contents
Install
npm:
npm install request-received
yarn:
yarn add request-received
Usage
Express
Symbols are automatically added to req
object in route middleware:
const express = require('express');
const Cabin = require('cabin');
const requestReceived = require('request-received');
const responseTime = require('response-time');
const requestId = require('express-request-id');
const startAt = Symbol.for('request-received.startAt');
const startTime = Symbol.for('request-received.startTime');
const app = express();
const Cabin = new Cabin();
app.use(requestReceived);
app.use((req, res, next) => {
console.log('startAt', req[startAt]);
console.log('startTime', req[startTime]);
next();
});
app.use(responseTime());
app.use(requestId());
app.use(cabin.middleware);
app.listen();
Koa
Symbols are automatically added to ctx
, ctx.req
, and ctx.request
objects in route middleware:
const Koa = require('koa');
const koaConnect = require('koa-connect');
const responseReceived = require('response-received');
const responseTime = require('response-time');
const requestId = require('express-request-id');
const startAt = Symbol.for('request-received.startAt');
const startTime = Symbol.for('request-received.startTime');
const app = new Koa();
const Cabin = new Cabin();
app.use(requestReceived);
app.use((ctx, next) => {
console.log('startAt', ctx[startAt]);
console.log('startTime', ctx[startTime]);
return next();
});
app.use(koaConnect(responseTime));
app.use(requestId());
app.use(cabin.middleware);
app.listen();
Contributors
License
MIT © Nick Baugh