All Projects → phil-r → stats

phil-r / stats

Licence: MIT License
📊 Request statistics middleware that stores response times, status code counts, etc

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to stats

Http Proxy Middleware
⚡ The one-liner node.js http-proxy middleware for connect, express and browser-sync
Stars: ✭ 8,730 (+58100%)
Mutual labels:  middleware, fastify, polka
Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+12840%)
Mutual labels:  middleware, koa
koa-waterline
Deprecated: A middleware for your hose
Stars: ✭ 14 (-6.67%)
Mutual labels:  middleware, koa
Redux Arc
A declarative way to make request with redux actions
Stars: ✭ 162 (+980%)
Mutual labels:  middleware, requests
Express Joi Validation
validate express application inputs and parameters using joi
Stars: ✭ 70 (+366.67%)
Mutual labels:  middleware, express-middleware
Service Tools
Prepare your Node.js application for production
Stars: ✭ 89 (+493.33%)
Mutual labels:  middleware, koa
Koa Hbs
Handlebars templates for Koa.js
Stars: ✭ 156 (+940%)
Mutual labels:  middleware, koa
Express Fileupload
Simple express file upload middleware that wraps around busboy
Stars: ✭ 1,069 (+7026.67%)
Mutual labels:  middleware, express-middleware
Express Basic Auth
Plug & play basic auth middleware for express
Stars: ✭ 241 (+1506.67%)
Mutual labels:  middleware, express-middleware
math-stats
A small library that does the statistics for your numbers.
Stars: ✭ 18 (+20%)
Mutual labels:  statistics, stats
polix
🚀 Node.js Web Framework
Stars: ✭ 32 (+113.33%)
Mutual labels:  middleware, koa
Koatty
Koa2 + Typescript = Koatty. Use Typescript's decorator implement IOC and AOP.
Stars: ✭ 67 (+346.67%)
Mutual labels:  middleware, koa
express-firebase-middleware
🔥 Express middleware for your Firebase applications
Stars: ✭ 53 (+253.33%)
Mutual labels:  middleware, express-middleware
Awilix Koa
Awilix helpers/middleware for Koa 2
Stars: ✭ 121 (+706.67%)
Mutual labels:  middleware, koa
Koa Useragent
Koa user-agent middleware
Stars: ✭ 54 (+260%)
Mutual labels:  middleware, koa
Request Migrations
HTTP Request Migrations for API Versioning like Stripe
Stars: ✭ 149 (+893.33%)
Mutual labels:  middleware, requests
Middlewares
💥 Middlewares / Relay / PSR-7 support to Nette Framework (@nette)
Stars: ✭ 13 (-13.33%)
Mutual labels:  middleware, requests
Connect Gzip Static
connect middleware for statically compressed files
Stars: ✭ 39 (+160%)
Mutual labels:  middleware, express-middleware
Host Validation
Express.js middleware for "Host" and "Referer" header validation to protect against DNS rebinding attacks.
Stars: ✭ 183 (+1120%)
Mutual labels:  middleware, express-middleware
mongodb-info
MongoDB Info - command line tool to get stats.
Stars: ✭ 13 (-13.33%)
Mutual labels:  statistics, stats

stats

Request statistics middleware

NPM version build status code coverage code style: prettier

Installation

npm i @phil-r/stats

API

const initStats = require('@phil-r/stats');
const { statsMiddleware, getStats } = initStats({
  endpointStats: true,
  complexEndpoints: ['/user/:id'],
  customStats: true,
  addHeader: true,
});

initStats([options])

Returns statsMiddleware middleware function and getStats function, that returns current stats

Options

initStats accepts optional options object that may contain any of the following keys:

endpointStats

Defaults to false

Boolean that indicates whether to track per endpoint stats.

complexEndpoints

Defaults to []

Used in conjunction with endpointStats

Use it in case your application has routes with params or wildcard routes

Recommended for applications that have endpoints like /user/123

customStats

Defaults to false

Adds startMeasurement and finishMeasurement functions to the request objects and allows measuring any parts of the app.

Usage:

function handler(req, res) {
  const measurement = req.startMeasurement('measurementName');
  // Some code...
  req.finishMeasurement(measurement);
}
addHeader

Defaults to false

Adds X-Response-Time header to all responses, can be used to replace expressjs/response-time

Example

Examples for popular node web frameworks can be found here

Here is the example of usage in express app:

const app = require('express')();
const initStats = require('@phil-r/stats');

const { statsMiddleware, getStats } = initStats({
  endpointStats: true,
  complexEndpoints: ['/user/:id'],
  customStats: true,
  addHeader: true,
});

app.use(statsMiddleware);
app.get('/', (req, res) => res.end('Hello'));
app.get('/user/:id', (req, res) => res.end(`Hello ${req.params.id}`));
app.get('/long', async (req, res) => {
  const measurement = req.startMeasurement('long');
  await new Promise((resolve) => {
    setTimeout(() => resolve(), 2000);
  });
  req.finishMeasurement(measurement);
  res.end(`Long job finished`);
});
app.get('/stats', (req, res) => res.send(getStats()));

app.listen(8080);
console.log('Server listens at http://localhost:8080');

Visiting http://localhost:8080/stats will give following result:

{
  "uptime": 63881,
  "uptimeHumanReadable": "1m 4s",
  "statusCodes": {
    "200": 5,
    "404": 4
  },
  "uuid": "330d9cc6-7d40-4964-888c-4d2817905ee1",
  "pid": 90603,
  "totalTime": 4020.3912830000004,
  "averageTime": 446.7101425555556,
  "count": 9,
  "endpointStats": {
    "GET /long": {
      "totalTime": 4009.410922,
      "averageTime": 2004.705461,
      "count": 2,
      "statusCodes": {
        "200": 2
      }
    },
    "GET /favicon.ico": {
      "totalTime": 4.286955,
      "averageTime": 1.07173875,
      "count": 4,
      "statusCodes": {
        "404": 4
      }
    },
    "GET /stats": {
      "totalTime": 6.227342999999999,
      "averageTime": 6.227342999999999,
      "count": 1,
      "statusCodes": {
        "200": 1
      }
    },
    "GET /user/:id": {
      "totalTime": 0.466063,
      "averageTime": 0.2330315,
      "count": 2,
      "statusCodes": {
        "200": 2
      }
    }
  },
  "customStats": {
    "long": {
      "totalTime": 4005.556455,
      "averageTime": 2002.7782275,
      "started": 2,
      "count": 2
    }
  }
}

All time related results are in milliseconds

License

This is a fork of zenmate/stats

Inspired by

expressjs/response-time and thoas/stats

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].