Registry / testing / run-middleware

run-middleware

JSON →
library0.9.10jsnpmunverified

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.

npm install run-middleware
INSTALL
IMPORT
SIG · RUN-MIDDLEWARE
R
run-middleware
testingjavascriptv0.9.10
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

run-middleware
require('run-middleware')(app)
import runMiddleware from 'run-middleware'
This package is CommonJS-only and mutates the Express app object via a side-effect function call. There is no default or named export; it adds the `runMiddleware` method to the app instance.
app.runMiddleware
app.runMiddleware('/path', options, callback)
app.runMiddleware({path:'/path', ...}, callback)
The method is attached to the Express app instance after calling require('run-middleware')(app). The first argument must be the route path string, not an options object.
res.runMiddleware
res.runMiddleware('/path', options, callback)
res.runMiddleware = app.runMiddleware
`res.runMiddleware` is available inside a middleware to pass cookies and headers from the current request. It is automatically attached by the module.

Demonstrates basic usage: setting up Express, requiring run-middleware to attach the method, then calling app.runMiddleware on a route to get the simulated response.

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); });
Debug
Known issues
deprecatedPackage 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.
affects: >=0.9.10
gotchaThe 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.
affects: >=0.6.1
gotchaThe 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;
affects: >=0.6.1
gotchaDoes 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})));
affects: >=0.6.1
Errors
Common errors & fixes
TypeError: app.runMiddleware is not a function
Missing call to require('run-middleware')(app) before using app.runMiddleware.
fix
Add `require('run-middleware')(app)` right after creating the Express app.
TypeError: Cannot read property 'path' of undefined
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: listen EADDRINUSE :::3000 after using run-middleware
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.
Upgrade
Version history
0.9.10latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
run-middleware — npm install run-middleware · libregistry