2.0.3 • Published 8 months ago

express-urlrewrite v2.0.3

Weekly downloads
162,821
License
MIT
Repository
github
Last release
8 months ago

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

js-api-starter@apemesh/ceamsanother-json-server@r35007/mock-server-litejson-server-custom-router@everything-registry/sub-chunk-1636xl-json-servervpoints-dev-app@d0whc3r/json-server@dony435/json-server@rsf/json-serverjdstructorjamplay-mongoose-elasticjson-server-forkjson-server-masterjson-server-plusjson-server-s3json-server-sinnbojson-server-testejson-server-cnjson-server-relationshipjson-server-routerjson-server-visopjson-server-with-batch-insertionjump-json-serverstructortranscend-chartstool-boxes@abstractball/json-server@crannbog/json-server@flowtr/proxybyrd@gift-adv/json-server@infinitebrahmanuniverse/nolb-express-u@masscode/json-server@nmyvision/json-server@mockly/core@mockly/server@jmilanes/hotbars@json-server/core@imemento/json-server@r35007/mock-server@qianzhong516/json-server-extended@progress/kendo-common-tasks@pageboard/imagemy-tnm-2mock-json-server-copymock-config-servermockmnano-gatewayon-httpon-http-kon-http-kkkon-http-y1on-http-y2on-http-y3on-http-y4on-http1on-http2on-taskgraphreact-dndnextreact-gui-builderimpro-kitimemento-json-serverhm-react-clihai-serverlyun-wanghong-frontendlowdb-middleware@wll8/json-server@whyour/qinglong@tsed/oidc-provider@thg303/json-server@types/express-urlrewrite@unicorn-dev/mssql-server@vostro/c2-module-webpack@zalastax/nolb-express-u@telerik/kendo-common-tasks@zlargon/json-serverad-json-serverdartonian-json-serverdal_funnypemo-json-serverenable-a11yexpress-starter0express0sfed-makerfds-json-serverfunnypfusebox-angular-universal-starter
2.0.3

8 months ago

2.0.2

1 year ago

2.0.1

2 years ago

2.0.0

3 years ago

1.4.0

4 years ago

1.3.0

5 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.0

11 years ago