npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

track-requests

v1.0.2

Published

HTTP requests monitors middleware for node.js

Downloads

55

Readme

Track-Requests NPM Version Build Status Coverage Status

HTTP request tracker middleware for node.js

A middleware that tracks http requests and save them into ElasticSearch in order to use Kibana as a Dashboard to give you the needed analytics of your application (sign up numbers, number of active users, errors tracking...)

Getting Started

Install the module with: npm install track-requests

const express = require('express');
const app = express();
const trackRequests = require('track-requests');
const options = {
  elasticSearchOptions: {
    elasticSearchUrl: 'http://127.0.0.1:9200/'
  }
};
app.use(trackRequests(options));

Documentation

trackRequests(options)

Create a new track request middleware function using the given options.
options is an object container for parameters used to customise the trackRequest middleware

options.getters

Object- Contains functions used to generate the tracking document

options.getters.getUser

Function- return String the http req user's Id. it takes as parameter req (The HTTP request) and res (The HTTP response)

default: () => 'anonymous'

Usage example:

let options = {
  getters: {
    getUser: (req, res) => {
      return req.user.id;
    }
  }
};

options.getters.getIp

Function- return String the http req user's Ip. it takes as parameter req (The HTTP request) and res (The HTTP response)

default: (req) => req.headers['x-real-ip']

options.getters.getUserAgent

Function- return String the http req user's userAgent. it takes as parameter req (The HTTP request) and res (The HTTP response)

default: (req) => req.headers['user-agent']

options.getters.getErrorMessage

Function- return String the error message when an error happens on the controller. it takes as parameter req (The HTTP request) and res (The HTTP response)

default: (req) => req.error

options.getters.getBody

Function- return Object http request body. it takes as parameter req (The HTTP request) and res (The HTTP response)

default: (req) => req.body

options.getters.getExtraFields

Function- return Object extra fields to be added to the generated tracking document. it takes as parameter req (The HTTP request) and res (The HTTP response)
PS: make sure to add a custom options.elasticSearchOptions.indexProperties that contains the extra fields parmeters

Usage example:

let options = {
  elasticSearchOptions: {
    indexProperties: {
      url: {'type': 'keyword'},
      method: {'type': 'keyword'},
      user: {'type': 'keyword'},
      ip: {'type': 'ip'},
      statusCode: {'type': 'short'},
      processingTime: {'type': 'integer'},
      createdAt: {'type': 'date'},
      error: {'type': 'text'},
      userAgent: {'type': 'text'},
      body: {'type': 'nested'},
      myCustomField: {'type': 'text'}
    }
  },
  getters: {
    getExtraFields: (req, res) => {
      return {
        myCustomField:' myCustomFieldValue'
      }
    }
  }
};

options.routesToIgnore

Array- array of String|Regex|Object of the endpoints that you want the tracker to ignore them

  • String it should match the exact endpoint's url to ignore it
  • Regex it should verify the endpoint's url to ignore it
  • Object all the parameters should matches to ignore the endpoint
    • url String it should match the exact endpoint
    • regex Regex it should verify the endpoint
    • statusCode Number it should match the HTTP response's Status Code
    • method String it should match the HTTP request's method

Usage example:

let options = {
  routesToIgnore:[
    /user/, //ignore all the endpoint that verify this regex 
    '/organization/member',//ignore this exact endpoint  
    {url: '/healthcheck', method: 'GET'},// ignore the GET requests to this exact endpoint    
    {regex: /search/, method: 'POST', statusCode: 404}// ignore the POST with a 404 response status code that verify this regex  
  ]
};

options.elasticSearchOptions

Object- Contains the parameters used to customise the elasticSearch client

options.elasticSearchOptions.elasticSearchUrl

String- ElasticSearch's Url (default: http://127.0.0.1:9200/)

options.elasticSearchOptions.indexName

String- ElasticSearch's Index Name (default: tracking)

options.elasticSearchOptions.indexProperties

Object- ElasticSearch's Index Properties, it represent the schema of the object that will be saved into elasticSearch Document.
It's used to:

  • setup the elasticSearch index when it does not already exists
  • make sure that the generated document that will be inserted into ES have the specified schema (any other parameter of the generated document will be deleted before the insertion)

default:

{
  "url": {"type" : "keyword"},
  "method": {"type" : "keyword"},
  "user": {"type": "keyword"},
  "ip": {"type": "ip"},
  "statusCode": {"type": "short"},
  "processingTime": {"type": "integer"},
  "createdAt": {"type": "date"},
  "error": {"type": "text"},
  "userAgent": {"type": "text"},
  "body": {"type": "nested"}
}  

MIT