connect-slashes

Trailing slash redirect middleware for Connect and Express.js

MIT 10 个版本
安装
npm install connect-slashes
yarn add connect-slashes
pnpm add connect-slashes
bun add connect-slashes
README

connect-slashes

Trailing slash redirect middleware for Connect and Express.js. Useful for creating canonical urls in your Node.js applications.

Installation

$ npm install connect-slashes

Usage

var connect = require("connect")
  , slashes = require("connect-slashes");

connect() // or express()
  .use(connect.static())
  .use(slashes()) // must come after static middleware!
  .listen(3000);

Alternatively, you can pass false as the first argument to .slashes() in order to remove trailing slashes instead of appending them:

.use(slashes(false));

Additional settings

You can also pass a second argument with an options object. For example, if an application is behind a reverse proxy server that removes part of the URL (a base_path) before proxying to the application, then the base can be specified with an option:

.use(slashes(true, { base: "/blog" })); // prepends a base url to the redirect

By default, all redirects are using the 301 Moved Permanently header. You can change this behavior by passing in the optional code option:

.use(slashes(true, { code: 302 })); // 302 Temporary redirects

You can also set additional headers to the redirect response with the headers option:

.use(slashes(true, { headers: { "Cache-Control": "public" } }));

Notes

  1. Only GET, HEAD, and OPTIONS requests will be redirected (to avoid losing POST/PUT data)
  2. This middleware will append or remove a trailing slash to all request urls. This includes filenames (/app.css => /app.css/), so it may break your static files. Make sure to .use() this middleware only after the connect.static() middleware.

LICENSE

MIT

版本列表
1.4.0 2019-03-21
1.3.1 2015-05-29
1.3.0 2014-10-06
1.2.0 2013-12-29
1.0.2 2013-11-19
0.0.11 2013-10-27
0.0.10 2012-11-18
0.0.9 2012-11-18
0.0.2 2012-07-07
0.0.1 2012-07-07