{"id":17265,"library":"http-rewrite-middleware","title":"HTTP Rewrite Middleware for Connect/Express","description":"This package, `http-rewrite-middleware`, provides Nginx-inspired, regular expression-based URL rewriting functionality as a middleware for Connect and Express web servers. It allows developers to define rules for internal rewrites (where the server processes the new URL internally without notifying the client) or external HTTP 301 (permanent) or 302 (temporary) redirects (where the client is instructed to request a new URL). The current version available is 0.1.6, though the provided README refers to v0.1.5. Its `node >= 0.8.0` engine requirement and low version number indicate that the package has likely not seen significant active development in many years, suggesting it is either abandoned or in a very minimal maintenance state. Its primary differentiators are its straightforward, rule-based approach for managing both internal URL transformations and client-side redirects, similar to how Nginx's `rewrite` module operates, making it a direct successor to `grunt-connect-rewrite` for Grunt-based setups.","status":"abandoned","version":"0.1.6","language":"javascript","source_language":"en","source_url":"git@github.com:viart/http-rewrite-middleware","tags":["javascript","gruntplugin","grunt-contrib-connect","grunt-express","modrewrite","rewriterule","rewrite","route","connect"],"install":[{"cmd":"npm install http-rewrite-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add http-rewrite-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-rewrite-middleware","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS module, primarily used with `require()`.","symbol":"rewriteModule","correct":"const rewriteModule = require('http-rewrite-middleware');"},{"note":"The `getMiddleware` function is the primary entry point, invoked from the module export.","symbol":"getMiddleware","correct":"const rewriteMiddleware = rewriteModule.getMiddleware([...rules]);"},{"note":"Rules are objects with `from` (RegExp string), `to` (replacement string), and optional `redirect` ('permanent' for 301, 'temporary' for 302).","symbol":"RuleOptions","correct":"{ from: '^/old$', to: '/new', redirect: 'permanent' }"}],"quickstart":{"code":"const express = require('express');\nconst rewriteModule = require('http-rewrite-middleware');\nconst app = express();\n\n// Define rewrite rules\nconst rewriteRules = [\n    // Internal rewrite: /old-path -> /new-path\n    {from: '^/old-path$', to: '/new-path'},\n    // Internal rewrite with capture group: /api/v1/users -> /api/v2/users\n    {from: '^/api/v1/(.*)$', to: '/api/v2/$1'},\n    // 301 Permanent Redirect: /legacy -> /modern\n    {from: '^/legacy$', to: '/modern', redirect: 'permanent'},\n    // 302 Temporary Redirect: /temp-page -> /under-construction\n    {from: '^/temp-page$', to: '/under-construction', redirect: 'temporary'}\n];\n\n// Apply the rewrite middleware first, with verbose logging\napp.use(rewriteModule.getMiddleware(rewriteRules, { verbose: true }));\n\n// Example routes after rewrite\napp.get('/new-path', (req, res) => {\n    res.send('Welcome to the new path (internal rewrite successful)!');\n});\n\napp.get('/api/v2/users', (req, res) => {\n    res.send('Accessing API v2 users endpoint.');\n});\n\napp.get('/modern', (req, res) => {\n    res.send('You have been permanently redirected here.');\n});\n\napp.get('/under-construction', (req, res) => {\n    res.send('This page is temporarily here.');\n});\n\napp.get('/', (req, res) => {\n    res.send('Hello from the root!');\n});\n\nconst PORT = 3000;\napp.listen(PORT, () => {\n    console.log(`Express server running on http://localhost:${PORT}`);\n    console.log('Try visiting the following URLs to see rewrites/redirects:');\n    console.log(`- http://localhost:${PORT}/old-path`);\n    console.log(`- http://localhost:${PORT}/api/v1/users`);\n    console.log(`- http://localhost:${PORT}/legacy`);\n    console.log(`- http://localhost:${PORT}/temp-page`);\n});","lang":"javascript","description":"This example demonstrates how to set up `http-rewrite-middleware` with Express, configuring both internal URL rewrites and HTTP 301/302 redirects with verbose logging for debugging."},"warnings":[{"fix":"Ensure `app.use(rewriteMiddleware)` is called early in your Connect/Express middleware stack, generally before `express.static()` or main router definitions.","message":"The order of middleware is crucial. `http-rewrite-middleware` should typically be placed before static file serving or other routing middleware to ensure rewrites occur before other handlers process the original URL.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always explicitly define the `redirect` option if you intend for the client's browser to update its URL or perform a visible redirect; otherwise, the change is internal to the server.","message":"Understand the difference between internal rewrites and HTTP redirects. Omitting the `redirect` option results in an internal rewrite (server handles the new path, client is unaware). Specifying `redirect: 'permanent'` or `redirect: 'temporary'` sends an HTTP 301 or 302 status, respectively, instructing the client to request the new URL.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Thoroughly test your rewrite rules with various URL patterns. Use tools like regex101.com to validate your regular expressions. Enable verbose logging (`getMiddleware(rules, { verbose: true })`) for detailed insights into matched rules.","message":"Regular expressions in the `from` field must be valid JavaScript RegExp strings. Incorrect or overly broad regular expressions can lead to unintended rewrites or redirects, affecting application behavior.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Verify that `http-rewrite-middleware` is listed in your `package.json` and installed (`npm install`). Ensure `const rewriteModule = require('http-rewrite-middleware');` is correctly placed and spelled.","cause":"The `http-rewrite-middleware` module was not correctly installed or imported, leading to `rewriteModule` being undefined.","error":"TypeError: Cannot read properties of undefined (reading 'getMiddleware') or similar"},{"fix":"Ensure that after a rewrite, the target URL (`__to__`) corresponds to an existing static file, a defined route, or another middleware capable of handling the request. For debugging, enable `verbose: true` to confirm which rules are matching.","cause":"The rewrite rule matched and redirected/rewrote to a new path, but there is no subsequent middleware or route handler configured to serve content at that new path.","error":"Cannot GET /original-url (or a rewritten URL) after applying middleware."}],"ecosystem":"npm","meta_description":null}