node package manager

koa-sanitize-uri

koa-sanitize-uri NPM version

Important note: v1.0.0 is now compliant with koa@2. If you use koa < 2, install koa-sanitize-uri 0.1.x

Purpose

koa-sanitize-uri is a KOA middleware to sanitize request uri.

Request URI are checked. If they have a bad format, the middleware throw a redirect to the good uri with a 301 status (by default).

It's basically a good practice to keep consistant with URI and have only one URI per page content. It's aim is to avoid duplicate content (same content on 2 URI).

Read the CHANGELOG

Usage

import koa from 'koa';
import koaSanitizeUri from 'koa-sanitize-uri';
 
const app= koa();
app.use(koaSanitizeUri());
 
// ... 
 
app.listen(1337);
 

Options

All options at once:

app.use(koaSanitizeUri({
  sanitize: {
    simpleChars : true,
    endingslash : false,
    doubleshash : true,
    lowercase : true,
  },
  code: 301,
  ignore: [/assets\/.*/, /.*\.(css|js|jpg|png)$/i]
}));

Ignore paths

You can give the middleware an option to ignore file or paths matching regex.

Example with one regex:

app.use(koaSanitizeUri({ignore: /\/assets/}));
 

Example with multi regex:

app.use(koaSanitizeUri({ignore: [/assets\/.*/, /.*\.(css|js|jpg|png)$/i]}));

Custom redirection status

app.use(koaSanitizeUri({status: 301}));

Sanitize options

You can pass all the sanitizer options with the key sanitize`

Developer

Installing dev dependencies, you can edit the package source. Then run:

  • npm run dev-compile for babel transpilation
  • npm run dev-check for eslint check