{"id":18766,"library":"run-middleware","title":"run-middleware","description":"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.","status":"maintenance","version":"0.9.10","language":"javascript","source_language":"en","source_url":"https://github.com/aminag/node-run-middleware","tags":["javascript"],"install":[{"cmd":"npm install run-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add run-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add run-middleware","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import runMiddleware from 'run-middleware'","symbol":"run-middleware","correct":"require('run-middleware')(app)"},{"note":"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.","wrong":"app.runMiddleware({path:'/path', ...}, callback)","symbol":"app.runMiddleware","correct":"app.runMiddleware('/path', options, callback)"},{"note":"`res.runMiddleware` is available inside a middleware to pass cookies and headers from the current request. It is automatically attached by the module.","wrong":"res.runMiddleware = app.runMiddleware","symbol":"res.runMiddleware","correct":"res.runMiddleware('/path', options, callback)"}],"quickstart":{"code":"const express = require('express');\nconst app = express();\n\n// Define a route\napp.get('/api/hello', (req, res) => {\n  res.json({ message: 'Hello, world!' });\n});\n\n// Initialize run-middleware\nrequire('run-middleware')(app);\n\n// Invoke the route programmatically\napp.runMiddleware('/api/hello', (code, body, headers) => {\n  console.log('Status:', code);\n  console.log('Body:', body);\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"Consider using supertest or direct function calls instead.","message":"Package has not been updated since 2018; may not be compatible with Express 4.x or newer versions.","severity":"deprecated","affected_versions":">=0.9.10"},{"fix":"Be aware that app.runMiddleware is added after require; avoid accidentally overriding it.","message":"The module mutates the Express app object by adding methods; this can cause side effects if not accounted for.","severity":"gotcha","affected_versions":">=0.6.1"},{"fix":"Parse body if needed: const parsed = typeof body === 'string' ? JSON.parse(body) : body;","message":"The callback signature is (code, body, headers) where body can be a string, Buffer, or object depending on middleware response.","severity":"gotcha","affected_versions":">=0.6.1"},{"fix":"Wrap in a Promise if needed: new Promise((resolve, reject) => app.runMiddleware(path, (code, body) => resolve({code, body})));","message":"Does not support async/await natively; callback pattern only.","severity":"gotcha","affected_versions":">=0.6.1"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add `require('run-middleware')(app)` right after creating the Express app.","cause":"Missing call to require('run-middleware')(app) before using app.runMiddleware.","error":"TypeError: app.runMiddleware is not a function"},{"fix":"Use `app.runMiddleware('/path', { query: {}, body: {} }, callback)` — path must be first argument.","cause":"Passing an options object as the first argument instead of a path string.","error":"TypeError: Cannot read property 'path' of undefined"},{"fix":"Do not call app.listen if only testing routes; runMiddleware does not require a listening server.","cause":"Running server.listen in the same test where runMiddleware is used without specifying port or async handling.","error":"Error: listen EADDRINUSE :::3000 after using run-middleware"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}