Skip to content

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.

License

Notifications You must be signed in to change notification settings

cabinjs/request-received

Repository files navigation

request-received

build status code coverage code style styled with prettier made with lass license npm downloads

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]); // `process.hrtime()`
  // [ 472542, 431456521 ]
  console.log('startTime', req[startTime]); // Date.now()
  // 1560499520000
  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 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 = new Koa();
const Cabin = new Cabin();

app.use(requestReceived);

app.use((ctx, next) => {
  console.log('startAt', ctx[startAt]); // `process.hrtime()`
  // [ 472542, 431456521 ]
  console.log('startTime', ctx[startTime]); // Date.now()
  // 1560499520000

  // note that the symbols are also accessible via:
  // ctx.req[startAt]
  // ctx.request[startAt]
  // ctx.req[startTime]
  // ctx.request[startTime]

  return next();
});

app.use(koaConnect(responseTime));
app.use(requestId());
app.use(cabin.middleware);

app.listen();

Contributors

Name Website
Nick Baugh http://niftylettuce.com/

License

MIT © Nick Baugh

About

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.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published