Giter VIP home page Giter VIP logo

express-slash's Introduction

Express Slash

Dependency Status npm Version

Express middleware for people who are anal about trailing slashes.

If you're a good person, then you enable Express' "strict routing" because you understand the difference between /about and /about/. You know that these URLs are not the same and they have different meanings. The trouble is, being a good person and caring about your trailing slashes is harder than not. Plus, you also care about other people, and it would be rude to 404 them when they forget the trailing slash. Luckily, there's this package to solve all your trailing slash problems :D

This Express middleware should come after your app's router middleware. It will handle [GET and HEAD] requests for URLs which did not have a matching route by either adding or removing a trailing slash to the URL's path, then checking the app's router for a matching route for the new URL, in which case it will redirect the client (301 by default) to that URL.

Note: Choose the correct version of this package for your Express version:

  • v1.x: Express 3.x
  • v2.x: Express 4.x

Installation

Install using npm:

$ npm install express-slash

Usage

Enable Express' "strict routing" setting, and add this middleware after your app's router middleware:

var express = require('express'),
    slash   = require('express-slash');

var app = express();

// Because you're the type of developer who cares about this sort of thing!
app.enable('strict routing');

// Create the router using the same routing options as the app.
var router = express.Router({
    caseSensitive: app.get('case sensitive routing'),
    strict       : app.get('strict routing')
});

// Add the `slash()` middleware after your app's `router`, optionally specify
// an HTTP status code to use when redirecting (defaults to 301).
app.use(router);
app.use(slash());

router.get('/', function (req, res) {
    res.send('Home');
});

router.get('/about/', function (req, res) {
    res.send('About');
});

router.get('/about/people', function (req, res) {
    res.send('People');
});

app.listen(3000);

Now when someone navigates to /about, they'll be redirected to /about/, and when someone navigates to /about/people/, they'll be redirected to /about/people.

License

This software is free to use under the MIT license. See the LICENSE file for license text and copyright information.

express-slash's People

Contributors

awakenalive avatar balaclark avatar ericf avatar truevektordev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.