req-meta-middleware

Express middleware for parsing request metadata (referrer, geo-ip, ..)


Keywords
express, middleware, referrer, geoip, useragent, utm
Install
npm install req-meta-middleware@0.1.4

Documentation

req-meta-middleware

An Express middleware for parsing various request metadata. The following data are parsed from the request object:

Installation

npm:

npm install req-meta-middleware

yarn:

yarn add req-meta-middleware

Usage

Add req-meta-middleware to your middleware stack. Note that we need to await for the middleware to initialize.

import meta from 'req-meta-middleware';

const app = express();
// ...
app.use(await meta());

All parsed information is stored at req.meta.

app.use((req: Request, res: Response, next: NextFunction) => {
  console.log(req.meta.referrer);
 });

Options

You can also specify options while initializing the middleware.

const options = {
   // Domains of the server (default: [])
  internalDomains: ['foo.com'],
  // When set to true, req IP will not be parsed and req.meta.ip will be null. (default: false)
  skipIP: false,
  // When set to true, the referrer database will be fetched from the web. (default: true)
  // Otherwise, local database stored in data/referers.json will be used.
  webReferralDB: true,
  // Cron schedule string for updating the referrer database.
  // This is only effective when webReferralDB is true. (default: '0 0 0 * * * *')
  updateSchedule: '0 0 0 * * * *'
};
server.use(meta(options));

See the node-cron docs for the cron schedule syntax.

Parsing after response

If req.meta isn't used for building responses, it may be faster in response time to parse the request after sending the response. (eg. only using metadata for referral stats) In that case, use later to parse metadata after the response has been sent.

const handler = (data) => {
  console.log(data);
};
server.use(await meta.later(handler, options));

The middleware will first call next() to further process the request, then parse and callback after the response has been sent.