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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lahin31/debugcontext-express

v0.1.2

Published

Express.js adapter for DebugContext — automatically captures request context, redacts sensitive data, and structures every route error as a debuggable Incident.

Readme

@lahin31/debugcontext-express

Express.js adapter for DebugContext — automatically captures every route error as a structured Incident with full request context, runtime info, system stats, and git metadata.

npm version npm downloads License: MIT


Install

npm install @lahin31/debugcontext-core @lahin31/debugcontext-express

Setup

import express from 'express';
import DebugContext from '@lahin31/debugcontext-core';
import DebugContextExpress from '@lahin31/debugcontext-express';

// 1. Initialise once at startup
DebugContext.init();

const app = express();
app.use(express.json());

// 2. Optional — mount before routes to enable route param capture
app.use(DebugContextExpress.requestMiddleware());

// 3. Your routes
app.get('/users/:id', (req, res) => {
  throw new Error('User not found'); // captured automatically
});

app.post('/login', (req, res) => {
  // passwords are auto-redacted in the incident
  throw new Error('Invalid credentials');
});

// 4. Mount error middleware AFTER all routes
app.use(DebugContextExpress.errorMiddleware());

app.listen(3000);

What gets captured

Every route error produces a full Incident:

{
  "incidentId": "3f2a1b4c-...",
  "timestamp": "2024-01-15T10:30:00.000Z",
  "request": {
    "method": "POST",
    "url": "/login",
    "params": {},
    "query": {},
    "body": {
      "username": "alice",
      "password": "[REDACTED]"
    },
    "headers": {
      "authorization": "[REDACTED]",
      "content-type": "application/json"
    },
    "ip": "127.0.0.1",
    "userAgent": "Mozilla/5.0 ..."
  },
  "runtime": { "nodeVersion": "v20.11.0", "environment": "production", "pid": 1234, "..." : "..." },
  "system": { "heapUsagePercent": 59.8, "...": "..." },
  "git": { "commitHash": "a1b2c3d4", "branch": "main", "packageVersion": "1.2.3" },
  "error": {
    "name": "Error",
    "message": "Invalid credentials",
    "stack": "Error: Invalid credentials\n    at ..."
  }
}

API

DebugContextExpress.errorMiddleware(options?)

Express 4-argument error middleware. Mount after all routes.

app.use(DebugContextExpress.errorMiddleware({
  // Pass the error to the next handler after capturing (default: true)
  rethrow: true,

  // Print incident to console (default: true in non-production)
  toConsole: true,
}));

The captured incident is also attached to req.debugContextIncident for use in downstream handlers:

app.use((err, req, res, next) => {
  const { incidentId } = req.debugContextIncident ?? {};
  res.status(500).json({ error: err.message, incidentId });
});

DebugContextExpress.requestMiddleware()

Optional middleware that enables route parameter capture even when Express clears req.params during error propagation.

Mount before your routes:

app.use(DebugContextExpress.requestMiddleware());

app.get('/users/:id', (req, res) => {
  throw new Error('not found');
  // incident.request.params.id === '42' ✓
});

Without this middleware, params will still be captured for next(err)-style async errors. It is only required for synchronous throws in parameterised routes.


Automatic Redaction

Sensitive data is always redacted before appearing in an Incident.

Headers: authorization, cookie, set-cookie, x-api-key, x-auth-token, and more.

Body fields: password, secret, token, apikey, access_token, refresh_token, credit_card, cvv, ssn, private_key, and more.

Add extra fields via DebugContext.init():

DebugContext.init({
  sensitiveHeaders: ['x-internal-token'],
  sensitiveFields: ['myCustomSecret'],
});

Send incidents anywhere

Use the onIncident hook to ship incidents to your own storage or alerting:

DebugContext.init({
  // Write to file
  onIncident: (i) => DebugContext.toFile(i, { path: 'logs/incidents.ndjson' }),

  // Send to a webhook
  onIncident: async (i) => {
    await fetch('https://my-alerting-service.example.com/incidents', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(i),
    });
  },
});

Related Packages


License

MIT © lahin31