中間件與路由處理器

路由處理器和中間件的參數中都有回調函數,這個函數有2個3個或4個參數。
若是有2個或3個參數,前兩個參數是請求和響應對象,第三個參數是next函數。
若是有4個參數,它就變成了錯誤處理中間件,第一個參數變成了錯誤對象,而後依次是請求、響應和next對象。
若是不調用next(),管道就會被終止,也不會再有處理器或中間件作後續處理,此時應該發送一個響應到客戶端。express

 1 var app = require('express')();
 2 let i=1;
 3 app.use(function(req, res, next){
 4     console.log('\nALLWAYS'+i++);
 5     next();
 6 });
 7 app.use(function(req, res, next){
 8     console.log('SOMETIMES');
 9     next();
10 });
11 //錯誤處理中間件
12 app.use(function(err, req, res, next){
13     console.log('unhandled error detected: ' + err.message);
14     res.send('500 - server error');
15 });
16 app.use(function(req, res){
17     console.log('route not handled');
18     res.send('404 - not found');
19 });
20 app.listen(3000, function(){
21     console.log('listening on 3000');
22 });

 

 1 var app = require('express')();
 2 let i=1;
 3 app.use(function(req, res, next){
 4       console.log('\nALLWAYS'+i++);
 5       next();
 6 });
 7 app.get('/a', function(req, res){
 8       console.log('/a: route terminated');
 9       res.send('a');
10 });
11 app.get('/a', function(req, res){
12       console.log('/a: never called');
13 });
14 app.use(function(req, res, next){
15       console.log('SOMETIMES');
16       next();
17 });
18 //錯誤處理中間件
19 app.use(function(err, req, res, next){
20       console.log('unhandled error detected: ' + err.message);
21       res.send('500 - server error');
22 });
23 app.use(function(req, res){
24       console.log('route not handled');
25       res.send('404 - not found');
26 });
27 app.listen(3000, function(){
28       console.log('listening on 3000');
29 });

 

 1 var app = require('express')();
 2 let i=1;
 3 app.use(function(req, res, next){
 4       console.log('\nALLWAYS'+i++);
 5       next();
 6 });
 7 app.get('/b', function(req, res, next){
 8       console.log('/b: route not terminated');
 9       next();
10 });
11 app.use(function(req, res, next){
12       console.log('SOMETIMES');
13       next();
14 });
15 app.get('/b', function(req, res, next){
16       console.log('/b (part 2): error thrown' );
17       throw new Error('b failed');
18 });
19 app.use('/b', function(err, req, res, next){
20       console.log('/b error detected and passed on');
21       next(err);
22 });
23 //錯誤處理中間件
24 app.use(function(err, req, res, next){
25       console.log('unhandled error detected: ' + err.message);
26       res.send('500 - server error');
27 });
28 app.use(function(req, res){
29       console.log('route not handled');
30       res.send('404 - not found');
31 });
32 app.listen(3000, function(){
33       console.log('listening on 3000');
34 });

 

 

 1 var app = require('express')();
 2 let i=1;
 3 app.use(function(req, res, next){
 4     console.log('\nALLWAYS'+i++);
 5     next();
 6 });
 7 app.use(function(req, res, next){
 8     console.log('SOMETIMES');
 9     next();
10 });
11 app.get('/c', function(err, req){
12     console.log('/c: error thrown');
13     throw new Error('c failed');
14 });
15 app.use('/c', function(err, req, res, next){
16     console.log('/c: error deteccted but not passed on');
17     next();
18 });
19 //錯誤處理中間件
20 app.use(function(err, req, res, next){
21     console.log('unhandled error detected: ' + err.message);
22     res.send('500 - server error');
23 });
24 app.use(function(req, res){
25     console.log('route not handled');
26     res.send('404 - not found');
27 });
28 app.listen(3000, function(){
29     console.log('listening on 3000');
30 });

 

相關文章
相關標籤/搜索