npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-to-slash

v1.0.0

Published

Redirect to / urls. (Express middleware.)

Downloads

18

Readme

express-to-slash

If you're anal about slashes, you should use this. I don't think '/admin' and '/admin/' should serve the same thing, if only because it makes relative links in the page point to completely different things.

Note: there is already express-slash which you use after your router so that it flip-flops between slash and non-slash.

This package is more specific. It only redirects from a non-slash to slash version of the path, and you have to be explicit about where you use it.

Synopsis

var app = express()
var toSlash = require('express-to-slash')

app.all('/admin', toSlash())
app.all('/blog', toSlash(301)) // Moved Permanently

This will redirect all .get(), .head(), .post() etc requests to /admin. It will also include any querystring which was included in the request.

e.g. /admin?hello=world will redirect to /admin/?hello=world

'strict routing' and 'strict'

In Express the default routing means that /path and /path/ map to the same route. I feel that this breaks how the web works, espectially if you use relative URLs in your templates. To fix this I think you need to deal with each route separately ... and of course in some cases you want to redirect one to the other. In my case, I sometimes redirect the non-slash version to the slash version, hence this project.

However, you still have to tell Express to be explicit about this. To do this on your app, you should do the following:

var app = express()
app.enable('strict routing')

In Express v4, you can now also create routers, but these too need to be told to be explicit about their routing. Therefore you should do the following when creating a new router:

var router = express.Router({
  'strict' : true,
})

Note: unfortunately the name of the option is different on the app and the router.

When using a router, you also need to put the redirect route first so that it is done prior to the other one. The reason for this is that usually a route is mounted using app.use() and therefore it is classed as middleware. Yet Express also tells us that middleware just checks the prefix of the path and is therefore not subject to the final slash. Yes, I think this is weird, but it is what it is! Therefore, try this:

var express = require('express')
var toSlash = require('express-to-slash')
// an express.Router({ 'strict' : true })
var myAdmin = require('./admin.js')

var app = express()
app.enable('strict routing')

app.use(toSlash('/admin/'))
app.use('/admin/', myAdmin)

Phew!

AUTHOR

Written by Andrew Chilton:

LICENSE

Copyright 2014 Andrew Chilton [email protected].

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

(Ends)