samedi 27 février 2016

Understanding middleware and route handler in Express.js

I am trying to understand how middleware and route handlers work in Express. In Web Development with Node and Express, the author gives and example of a interesting route and middleware in action, but does not give the actual details.

Can some one please help me understand what happens at each step in the following example so that I can be sure that I thinking correctly about it? Here's the example:

var app = require('express')();
app.use(function(req, res, next){ console.log('\n\nALLWAYS');
next(); });
app.get('/a', function(req, res){ console.log('/a: route terminated'); res.send('a');
});

app.get('/a', function(req, res){
            console.log('/a: never called');
});

app.get('/b', function(req, res, next){
            console.log('/b: route not terminated');
next(); });

app.use(function(req, res, next){ console.log('SOMETIMES');
next(); });

app.get('/b', function(req, res, next){ console.log('/b (part 2): error thrown' ); throw new Error('b failed');
});

app.use('/b', function(err, req, res, next){ console.log('/b error detected and passed on'); next(err);
});

app.get('/c', function(err, req){
console.log('/c: error thrown'); throw new Error('c failed');
});

app.use('/c', function(err, req, res, next){
            console.log('/c: error deteccted but not passed on');
next(); });

app.use(function(err, req, res, next){
console.log('unhandled error detected: ' + err.message); res.send('500 - server error');
});

app.use(function(req, res){ console.log('route not handled'); res.send('404 - not found');
});

app.listen(3000, function(){
            console.log('listening on 3000');
});




Aucun commentaire:

Enregistrer un commentaire