中间件与路由处理器
Posted 火磷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中间件与路由处理器相关的知识,希望对你有一定的参考价值。
路由处理器和中间件的参数中都有回调函数,这个函数有2个3个或4个参数。
如果有2个或3个参数,前两个参数是请求和响应对象,第三个参数是next函数。
如果有4个参数,它就变成了错误处理中间件,第一个参数变成了错误对象,然后依次是请求、响应和next对象。
如果不调用next(),管道就会被终止,也不会再有处理器或中间件做后续处理,此时应该发送一个响应到客户端。
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 });
以上是关于中间件与路由处理器的主要内容,如果未能解决你的问题,请参考以下文章