• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Express middleware for people who are anal about trailing slashes.

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.

More Repositories

1

express-handlebars

A Handlebars view engine for Express which doesn't suck.
JavaScript
2,314
star
2

open-marriage

The open source website for Leslie Verploegen's and Eric Ferraiuolo's wedding
JavaScript
217
star
3

css-mediaquery

Parses and determines if a given CSS Media Query matches a set of values via JavaScript.
JavaScript
112
star
4

photosnear.me

Photos Near Me
JavaScript
99
star
5

urljs

[DEPRECATED] An API for working with URLs in JavaScript
JavaScript
75
star
6

timebox

This app helps TC39 stay on agenda during meetings.
JavaScript
20
star
7

grunt-css-selectors

Mutate CSS selectors.
JavaScript
19
star
8

grunt-broccoli-build

Runs a Broccoli build via Grunt.
JavaScript
13
star
9

rework-mutate-selectors

Mutate CSS selectors via Rework.
JavaScript
6
star
10

button.css

The Holy Grail of CSS buttons
6
star
11

nowww

A simple Node.js server that responds to `www.` requests with a redirect to the same URL without the pointless `www.` subdomain.
JavaScript
6
star
12

square-thing-app

Super simple example app using YUI's Y.App component.
JavaScript
6
star
13

formatjs-express-handlebars

Example Express app using FormatJS and Handlebars
JavaScript
5
star
14

amazon-silk-findings

A collection of findings that pertain to web developers who are supporting the interesting hybrid web browser on the Kindle Fire.
4
star
15

yui3-grids-annotations

A bookmarklet that annotates the grid structure of a web page using YUI 3 CSS Grids.
JavaScript
4
star
16

require-overhead

Test the overhead of the ES6 Module bundle format vs Browserify's and Webpack's CommonJS format.
JavaScript
2
star
17

basecss

A Baseline CSS for starting new projects
2
star
18

189oakstreet.com

2
star
19

ericf.github.com

My GitHub pages
1
star
20

enviroecon.org

Website for the Investing in a Sustainable Future Conference.
HTML
1
star
21

pjax-blacklist

YUI Pjax blacklist example
JavaScript
1
star
22

yapp-react

JavaScript
1
star
23

express-extend

Extend Express apps in the most compatible way
JavaScript
1
star