run-middleware
raw JSON → 0.9.10 verified Sat Apr 25 auth: no javascript maintenance
A Node.js module to programmatically execute Express middleware and route handlers without making network requests. It simulates a client calling REST APIs internally, allowing direct invocation of endpoints from code with support for custom query parameters, body, HTTP method, cookies, and response redirect tracking. Current stable version is 0.9.10 (last published in 2018). It has low maintenance activity, with no recent updates. Key differentiator: enables server-side execution of Express routes without requiring a listening server, useful for testing or reuse of logic.
Common errors
error TypeError: app.runMiddleware is not a function ↓
cause Missing call to require('run-middleware')(app) before using app.runMiddleware.
fix
Add
require('run-middleware')(app) right after creating the Express app. error TypeError: Cannot read property 'path' of undefined ↓
cause Passing an options object as the first argument instead of a path string.
fix
Use
app.runMiddleware('/path', { query: {}, body: {} }, callback) — path must be first argument. error Error: listen EADDRINUSE :::3000 after using run-middleware ↓
cause Running server.listen in the same test where runMiddleware is used without specifying port or async handling.
fix
Do not call app.listen if only testing routes; runMiddleware does not require a listening server.
Warnings
deprecated Package has not been updated since 2018; may not be compatible with Express 4.x or newer versions. ↓
fix Consider using supertest or direct function calls instead.
gotcha The module mutates the Express app object by adding methods; this can cause side effects if not accounted for. ↓
fix Be aware that app.runMiddleware is added after require; avoid accidentally overriding it.
gotcha The callback signature is (code, body, headers) where body can be a string, Buffer, or object depending on middleware response. ↓
fix Parse body if needed: const parsed = typeof body === 'string' ? JSON.parse(body) : body;
gotcha Does not support async/await natively; callback pattern only. ↓
fix Wrap in a Promise if needed: new Promise((resolve, reject) => app.runMiddleware(path, (code, body) => resolve({code, body})));
Install
npm install run-middleware yarn add run-middleware pnpm add run-middleware Imports
- run-middleware wrong
import runMiddleware from 'run-middleware'correctrequire('run-middleware')(app) - app.runMiddleware wrong
app.runMiddleware({path:'/path', ...}, callback)correctapp.runMiddleware('/path', options, callback) - res.runMiddleware wrong
res.runMiddleware = app.runMiddlewarecorrectres.runMiddleware('/path', options, callback)
Quickstart
const express = require('express');
const app = express();
// Define a route
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello, world!' });
});
// Initialize run-middleware
require('run-middleware')(app);
// Invoke the route programmatically
app.runMiddleware('/api/hello', (code, body, headers) => {
console.log('Status:', code);
console.log('Body:', body);
});