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

express-api-mockr

v1.0.1

Published

Powerful Express middleware to mock REST APIs with dynamic responses, per-route latency simulation, and request-aware body generation. Perfect for frontend development without a real backend.

Readme

express-api-mockr

Powerful Express middleware to mock REST APIs with dynamic responses, per-route latency simulation, and request-aware body generation. Perfect for frontend development without a real backend.

npm version license


Features

  • Route-Based Mocking -- Define responses by URL path and HTTP method (GET, POST, PUT, DELETE).
  • Dynamic Responses -- Use functions to generate responses based on the incoming request object.
  • Latency Simulation -- Add global or per-route delays to simulate real network latency.
  • Status Codes -- Return any HTTP status code per route.
  • Verbose Logging -- Optional console logging of matched routes for debugging.
  • Express Native -- Drop-in middleware compatible with Express 4 and 5.

Installation

npm install express-api-mockr

Usage

Basic Setup

const express = require('express');
const apiMock = require('express-api-mockr');

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

app.use(apiMock({
  endpoints: {
    '/api/users': {
      get: {
        status: 200,
        body: [
          { id: 1, name: 'Alice', role: 'Admin' },
          { id: 2, name: 'Bob', role: 'User' },
          { id: 3, name: 'Charlie', role: 'Editor' }
        ]
      }
    }
  }
}));

app.listen(3000, () => {
  console.log('Mock server running on http://localhost:3000');
});

Dynamic Responses

Use a function for the body property to generate responses based on the request.

app.use(apiMock({
  endpoints: {
    '/api/users': {
      post: {
        status: 201,
        body: (req) => ({
          id: Math.floor(Math.random() * 10000),
          name: req.body.name,
          email: req.body.email,
          createdAt: new Date().toISOString()
        })
      }
    },
    '/api/search': {
      get: {
        body: (req) => ({
          query: req.query.q,
          results: [],
          timestamp: Date.now()
        })
      }
    }
  }
}));

Simulating Latency

app.use(apiMock({
  delay: 500, // Global 500ms delay for all routes
  endpoints: {
    '/api/fast': {
      get: {
        delay: 0, // Override: no delay for this route
        body: { message: 'instant response' }
      }
    },
    '/api/slow': {
      get: {
        delay: 3000, // Override: 3 second delay for this route
        body: { message: 'slow response' }
      }
    }
  }
}));

Error Simulation

app.use(apiMock({
  endpoints: {
    '/api/protected': {
      get: {
        status: 401,
        body: { error: 'Unauthorized', message: 'Token required' }
      }
    },
    '/api/broken': {
      default: {
        status: 500,
        body: { error: 'Internal Server Error' }
      }
    }
  }
}));

Verbose Logging

app.use(apiMock({
  verbose: true, // Logs "[Mocker] Matching GET /api/users" to console
  endpoints: { /* ... */ }
}));

Fallthrough Behavior

If a request does not match any defined endpoint, the middleware calls next() and passes control to the next middleware or route handler in the stack.


API Reference

apiMock(config)

| Option | Type | Default | Description | | --- | --- | --- | --- | | endpoints | Object | {} | Route definitions keyed by path | | delay | number | 0 | Global delay in milliseconds | | verbose | boolean | false | Log matched routes to console |

Endpoint Definition

{
  '/api/path': {
    get: { status: 200, body: { ... }, delay: 0 },
    post: { status: 201, body: (req) => ({ ... }) },
    default: { status: 404, body: { error: 'Not found' } }
  }
}

License

MIT -- see LICENSE for details.