app.get('/about/', function(req, res) { res.redirect('/about'); });
However, I'd suggest skipping the redirect and just doing this:
app.get('/about/?', function(req, res) { res.send('hello'); });
The '/?' makes the trailing slash optional and will serve both /about and /about/. Adding a redirect in there just increases traffic and server load while making your site or application feel less responsive as users sit waiting for that invisible extra HTTP round trip.
> Ideally in turn, if I created the route:
>
> app.get('/users/', function(req, res) {
> res.send('list of users');
> });
>
> Requests to '/users' would be redirected to '/users/'.
If you are going to do the redirect thing, pick one convention and stick with it :-) Why would you want some requests to require a trailing slash and others require it not to be there?
> Would this be feasible to do using middleware?
If you are sticking to a single convention the sure: create a middleware that calls res.redirect() if the trailing slash is missing (or present), next() otherwise. If you want to mix conventions you would need to figure out, for each request, which routes match it, which do (or don't) include a trailing slash, and what to do in the case where multiple routes match and they aren't consistent -- does the routes with trailing slash win, or the ones without?
Perhaps you could elaborate on your use-case and why you need to do this?
--
Laurie Harper
http://laurie.holoweb.net/
However you are probably better off using the request params and then
handling the various paths in a switch/case block:
app.get('/:page?', function(req, res){
switch (req.params.page){
case 'about':
res.send('about');
break;
case 'stuff':
res.send('stuff');
break;
default:
res.send('default')
}
});
> --
> You received this message because you are subscribed to the Google Groups "Express" group.
> To post to this group, send email to expre...@googlegroups.com.
> To unsubscribe from this group, send email to express-js+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/express-js?hl=en.
>
>
As far as I know, REST is just a set of architectural patterns, not a specification, and is not specifically tied to the web, or even HTTP for that matter, even if it's almost always mentioned in that context.
L.
I feel like this is already well handled by your web server (mod_rewrite and similar), so why burden the app logic with it at all?
Richard, did you ever find a solution for this? I'm a bit surprised that this functionality isn't included (in middleware or otherwise). This is basic canonicalization.
To respond with 200 (and same content) to both /users and /users/, is to duplicate content... bad for seo. You need to pick one way as the canoncial representation, and 301 redirect the other. Generally, I would be much more worried about seo than any impact on performance this redirect would have.
To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/m3LX6ynZ-lIJ.