catch-all route handler

9,379 views
Skip to first unread message

bluesman

unread,
Nov 5, 2010, 1:36:33 AM11/5/10
to Express
I'm trying to override the default 404 behavior in express and I can't
quite figure out how to do it. when my app encounters a url that is
not supported, i get a 404 but it's a canned response: Cannot GET /foo

I'd like to have my own custom 404 page whenever an unhandled route is
encountered.

my app is structured so that i use individual scripts to handle
specific routes - call them controllers:

app.use('/',require('./controllers/index'));
app.use('/profile',require('./controllers/profile));
app.use('/search',require('./controllers/search));
etc...

so that URI: /profile/view/123
would invoke a function view() in ./controllers/profile/index.js

..that's all pretty standard.

how do i specify a catch-all controller if the URI doesn't match any
of the defined route handlers?

so if someone requested a URI that is not tied to a controller i could
dispatch say, ./controllers/error/index.js

if this has already been answered, could someone point me to that
post? i haven't found it!

thanks!

-t

vision media [ Tj Holowaychuk ]

unread,
Nov 5, 2010, 4:05:54 AM11/5/10
to expre...@googlegroups.com
all you have to do is add middleware at the bottom, and if the request gets through you can consider it a 404. For example:

  app.use(function(req, res){
    res.send(404);
  });


--
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.




--
Tj Holowaychuk
Vision Media
President & Creative Lead

bluesman

unread,
Nov 5, 2010, 12:29:10 PM11/5/10
to Express
thank you - makes perfect sense - i don't know why that didn't click
for me!

On Nov 5, 1:05 am, "vision media [ Tj Holowaychuk ]" <t...@vision-
> > express-js+...@googlegroups.com<express-js%2Bunsu...@googlegroups.com>
> > .

bluesman

unread,
Nov 6, 2010, 6:05:00 PM11/6/10
to Express
ok for future reference - i ended up with something like this as my
default 404 handler which allows me to use my layout template and
still return status 404:

app.use(function(req,res) {
res.render('404',
{
locals: {'title':'Not Found'},
},
function(err,str) {
res.send(str,404);
}
);

in hindsight its pretty obvious, but it might save some newbs like me
a few mins if you mention in the express documentation that the render
callback takes the rendered string as an arg..

thanks for all the help!

-t

vision media [ Tj Holowaychuk ]

unread,
Nov 6, 2010, 6:06:56 PM11/6/10
to expre...@googlegroups.com
you can also pass { status: 404 } as an option to render() not sure if I have docs on that

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.

vision media [ Tj Holowaychuk ]

unread,
Nov 6, 2010, 6:07:16 PM11/6/10
to expre...@googlegroups.com

gnim

unread,
Nov 16, 2010, 12:17:11 PM11/16/10
to Express
Sorry for co-opting, started with node.js a couple of hours ago and
hooked already;

app.use(function(req, res){
res.send('404 - Not found');
console.log('Not found!?!');
});

-will always log the last message, does that mean res.send() is called
as well, but without any effect, or am I missing some vital clues
about send() and render() ?

I was initially using the 'NotFound(msg)' error object from the Error
Handling section of the guide inside this function like:

app.use(function(req, res){
throw new NotFound;
});

Most likely I am missing some part of some fundamental concept for
node.js =)

Stéphane

unread,
Nov 16, 2010, 3:36:10 PM11/16/10
to expre...@googlegroups.com
Hi,

I've found a way to catch all GET requests if they havnt been satisfied by any other routes before :

app.get('*', function(req, res){//code here});

You must add it at the very end

Regards,


Before Printing, Think about Your Environmental Responsibility!
Avant d'Imprimer, Pensez à Votre Responsabilitée Environnementale!


vision media [ Tj Holowaychuk ]

unread,
Nov 16, 2010, 4:41:58 PM11/16/10
to expre...@googlegroups.com
keep in mind you can use middleware for this as well.. :p anything after app.use(app.router) will catch requests that have not been satisfied as well, or you can use the new app.all() which will work with any http verb
Reply all
Reply to author
Forward
0 new messages