{"id":17803,"library":"middleware-proxy","title":"Connect/Express HTTP Proxy Middleware","description":"middleware-proxy is a connect-compliant HTTP middleware designed for transparently redirecting incoming requests, primarily useful in development environments where a frontend served by Node.js needs to proxy API calls to a separate backend server. It simplifies integrated project development by allowing developers to access static assets through a local Node server while redirecting RESTful API calls to an external backend. The current stable version is 2.0.5, with recent releases focusing on bug fixes like handling HTTPS and URL parsing edge cases. The package provides a flexible API that supports regular expressions for URL matching and offers an optional feature to strip URL prefixes before forwarding requests, making it suitable for managing API gateway differences between development and production setups.","status":"maintenance","version":"2.0.5","language":"javascript","source_language":"en","source_url":"https://github.com/probablyup/middleware-proxy","tags":["javascript","http","proxy","server","connect","express","middleware"],"install":[{"cmd":"npm install middleware-proxy","lang":"bash","label":"npm"},{"cmd":"yarn add middleware-proxy","lang":"bash","label":"yarn"},{"cmd":"pnpm add middleware-proxy","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS module. Direct ESM 'import' syntax is not supported and will result in a runtime error. Use 'require()' or a build tool configured for CJS interop.","wrong":"import proxy from 'middleware-proxy';","symbol":"proxy","correct":"const proxy = require('middleware-proxy');"}],"quickstart":{"code":"const express = require('express');\nconst app = express();\nconst http = require('http');\nconst proxy = require('middleware-proxy');\n\n// Basic Express setup\napp.set('port', process.env.PORT || 3000);\napp.use(express.static(__dirname + '/app')); // Serve static files from './app'\n\n// Proxy requests starting with '/service' to http://localhost:8080\napp.use(proxy('/service', 'http://localhost:8080'));\n\nhttp.createServer(app).listen(app.get('port'), function() {\n    console.log(`Express server listening on port ${app.get('port')}`);\n});\n","lang":"javascript","description":"Demonstrates how to integrate middleware-proxy with an Express application to redirect requests starting with '/service' to a backend server."},"warnings":[{"fix":"Update usage from `app.use(function() { return proxy(...) })` to `app.use(proxy(...))` directly.","message":"Major refactor in v2.0.1 changed the return signature of the main export. Prior to v2.0.1, 'middleware-proxy' returned a function that *created* the middleware. Since v2.0.1, it directly returns the middleware function itself.","severity":"breaking","affected_versions":"<2.0.1"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Run `npm install middleware-proxy` or `yarn add middleware-proxy`.","cause":"The package has not been installed in your project.","error":"Error: Cannot find module 'middleware-proxy'"},{"fix":"Ensure you are using `const proxy = require('middleware-proxy');` for CommonJS. If upgrading from `<v2.0.1`, ensure you are now using `app.use(proxy(...))` directly without an outer function wrapper.","cause":"This error often occurs when attempting to use ESM `import` syntax with a CommonJS module, or incorrect usage after the v2.0.1 refactor (e.g., trying to call `proxy` twice to get the middleware).","error":"TypeError: proxy is not a function"},{"fix":"Double-check the 'matcher' argument to ensure it correctly identifies the desired URLs (e.g., `/api/v1` for `http://localhost:8080/api/v1`). Verify the 'server' URL is accurate and accessible. Ensure 'path_to_strip' correctly removes the desired prefix without altering the base path incorrectly (e.g., stripping `/api` from `/api/posts` to become `/posts`).","cause":"Incorrect 'matcher' string or regular expression, incorrect 'server' URL, or 'path_to_strip' misconfiguration.","error":"Requests are not being proxied as expected, or are resulting in 404s/incorrect data."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}