Skip to content

vgno/koa-normalize-path

Repository files navigation

koa-normalize-path

Koa middleware that normalizes paths.

Notice: koa-normalize-path@2 supports koa@2; if you want to use this module with koa@1, please use koa-normalize-path@1.

Build Status Coverage Status npm

Installation

npm install koa-normalize-path

API

const Koa = require('koa');
const app = new Koa();
app.use(require('koa-normalize-path')(opts));
  • opts options object.

Options

  • defer - If true, serves after yield next, allowing any downstream middleware to respond first. Defaults to true.
  • chained - If the middleware should continue modifying the url if it detects that a redirect already have been performed. Defaults to true.

Example

const Koa = require('koa');
const normalizePath = require('koa-normalize-path');

const app = new Koa();

app.use(normalizePath());

app.use(ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

Important

When used together with koa-static it might be a good idea to set defer to true for koa-static and false for koa-normalize-path. This to avoid a potential problem that will result in an error being thrown stating the path is malicious if it starts with multiple slashes. koajs/send#51

License

MIT