catch-all route handler

Showing 1-9 of 9 messages
catch-all route handler bluesman 11/4/10 10:36 PM
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
Re: [Express-js] catch-all route handler vision media [ Tj Holowaychuk ] 11/5/10 1:05 AM
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
Re: catch-all route handler bluesman 11/5/10 9:29 AM
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-
media.ca> wrote:
> 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);
>   });
>
>
>
> > express-js+...@googlegroups.com<express-js%2Bunsubscribe@googlegroups.com>
> > .
Re: catch-all route handler bluesman 11/6/10 3:05 PM
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
Re: [Express-js] Re: catch-all route handler vision media [ Tj Holowaychuk ] 11/6/10 3:06 PM
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.

Re: [Express-js] Re: catch-all route handler vision media [ Tj Holowaychuk ] 11/6/10 3:07 PM
ah, yes i do http://expressjs.com/guide.html#res-render-view-options-fn-
Re: catch-all route handler gnim 11/16/10 9:17 AM
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 =)
Re: [Express-js] Re: catch-all route handler Stéphane Erard 11/16/10 12:36 PM
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!



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


Re: [Express-js] Re: catch-all route handler vision media [ Tj Holowaychuk ] 11/16/10 1:41 PM
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