K
K
Keenest2012-06-11 16:19:46
Node.js
Keenest, 2012-06-11 16:19:46

Routing, or to whom does next pass control?

Good day, colleagues.
With such routing, everything is clear -

app.get('/', middleWare, function (req, res) {
    res.render('index', { s: 'hello' });
});

, where middleWare can be an array of nested functions, each of which has a next() call. And if you call next without parameters, then control is transferred further (and in this case we will get into res.render), otherwise ...
This is where the question arises, what else?
By default, the text of the error is displayed directly in the form of text on the console and the user, so I made such “wrappers” like
'someMiddlewareFunction': function (errfunc) {             
            return function (req, res, next) {              
// .. code
                    if (someFlag) {
                        errfunc(global.ERR_AUTH_NOT_LOGGED_IN, req, res);
                    }
                    else {
                        next();
                    }
// .. code
        }

Well, then I just transferred another function to this function, which rendered me a beautiful page with the number 500 and a description of the error.
But how correct is this approach?
And yet - where is control generally transferred when calling next ('ERROR') in such a context? —
app.get( '/users/:id([0-9]+)', function (req, res, next) {
    if (id > 10) {
// .. some code
        }
        else {
            next('ERROR');            
        }
});

And how to catch such errors?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pomeo, 2012-06-11
@Keenest

github.com/visionmedia/express/blob/master/examples/error-pages/index.js

K
kuzemchik, 2012-06-11
@kuzemchik

As far as I understand

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});

app.configure('production', function(){
  app.use(express.errorHandler()); 
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question