• 中间件与路由处理器


    路由处理器和中间件的参数中都有回调函数,这个函数有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('
    ALLWAYS'+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('
    ALLWAYS'+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('
    ALLWAYS'+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('
    ALLWAYS'+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 });

     

  • 相关阅读:
    ceph故障恢复
    上线遇到nginx问题
    java.lang.IllegalArgumentException: Comparison method violates its general contract 异常
    drone构建build时mvn日志太多,取消日志打印,输出失败异常
    mongo异常com.mongodb.MongoCursorNotFoundException
    循环list执行删除报ConcurrentModificationException异常
    磁盘目录/dev/vda2满了进行清理
    elasticSearch基本操作
    LOJ#6029「雅礼集训 2017 Day1」市场
    虚树
  • 原文地址:https://www.cnblogs.com/jfl-xx/p/7239792.html
Copyright © 2020-2023  润新知