1. express-urlrewrite
URL rewrite middleware for express
express-urlrewrite
Package: express-urlrewrite
Created by: kapouer
Last modified: Sat, 13 Apr 2024 20:46:55 GMT
Version: 2.0.2
License: MIT
Downloads: 1,181,665
Repository: https://github.com/kapouer/express-urlrewrite

Install

npm install express-urlrewrite
yarn add express-urlrewrite

express-urlrewrite

URL rewrite middleware for express.

Examples

Rewrite using a regular expression, rewriting /i123 to /items/123.

 app.use(rewrite(/^\/i(\w+)/, '/items/$1'));

Rewrite using route parameters, references may be named
or numeric. For example rewrite /foo..bar to /commits/foo/to/bar:

 app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));

You may also use the wildcard * to soak up several segments,
for example /js/vendor/jquery.js would become
/public/assets/js/vendor/jquery.js:

 app.use(rewrite('/js/*', '/public/assets/js/$1'));

In the above examples, the original query string (if any) is left untouched.
The regular expression is applied to the full url, so the query string
can be modified as well:

 app.use(rewrite('/file\\?param=:param', '/file/:param'))

The query string delimiter (?) must be escaped for the regular expression
to work.

New in version 1.1

 app.use(rewrite('/path', '/anotherpath?param=some'))

now updates req.query, so req.query.param == 'some'.

New in version 1.2

rewrite can be used as a route middleware as in

 app.get('/route/:var', rewrite('/rewritten/:var'));

app.get('/rewritten/:var', someMw);

Instead of passing control to next middleware, it passes control to next route.

Debugging

Set environment variable DEBUG=express-urlrewrite

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin