在express.js上使用一系列中间件 [英] Use an array of middlewares at express.js

查看:55
本文介绍了在express.js上使用一系列中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一系列中间件.好吧,更像是函数名和数组的组合.

I'm trying to use an array of middlewares. Well, more like a combination of function names and arrays.

代替:

router.post('/editPassword', validate, changePassword, sendConfirmation);

我想要类似的东西:

router.post('/editPassword', validate, [changePassword, sendConfirmation] ); 

这看起来像:

router.post('/editPassword', validate, doAction ); 

其中doAction将是这样的数组:

var doAction = [
   //equivalent of changePassword
   function(req, res, next){
      //whatever
      next();
   },

   //equivalent to the previous sendConfirmation
   function(req, res, next){
      //whatever
   }
]

但是似乎它失败了,并返回到doAction中第一个函数中next()之后的validate步骤.

But it seems it is failing and going back to the validate step after the next() within the first function in doAction.

我正在寻找一种简化中间件链接的方法,其中包括使用相同名称的一些中间件步骤.

I'm looking for a way to simplify the middleware chaining including some middleware steps under the same name.

推荐答案

我认为您希望它看起来像这样的原因不仅在于它看起来像样,而且还能够重用其他中间件.在这种情况下,您可以创建一个中间件,该中间件运行所有其他中间件来为您做检查,并且仅在所有验证成功后才调用下一个函数.

I assume the reason you wanted it to look that way is not only for it to appear presentable, but also to be able to reuse the other middleware. In that case, you can create a middleware which runs all other middlewares to do the check for you, and only calls the next function if all validations succeed.

var express = require('express');
var app = express();

function middleware1(req, res, next) {
    if(req.query.num >= 1) {
        next();
    } else {
        res.json({message: "failed validation 1"});
    }
}

function middleware2(req, res, next) {
    if(req.query.num >= 2) {
        next();
    } else {
        res.json({message: "failed validation 2"});
    }
}

function middleware3(req, res, next) {
    if(req.query.num >= 3) {
        next();
    } else {
        res.json({message: "failed validation 3"});
    }
}

function combination(req, res, next) {
    middleware1(req, res, function () {
        middleware2(req, res, function () {
            middleware3(req, res, function () {
                next();
            })
        })
    })
}


app.get('/', combination, function (req, res) {
  res.send('Passed All Validation!');
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

您可以通过运行该应用程序然后查看http://localhost:3000/?num=3,将值3更改为较小的数字,或删除num参数来测试该应用程序.

You can test this app by running it then viewing http://localhost:3000/?num=3, changing the value 3 to a lower number, or removing the num parameter.

我不确定这是否是正确的方法,但这就是我处理其他项目的方式.让我知道你的想法.

I'm not sure if this is the proper way to do it, but this is how I've handled my other projects. Let me know what you think.

注释:查看用例注释.根据您想要使用中间件的方式,@ robertklep可能会有更好的解决方案

note: see comments for use case. @robertklep may have a better solution depending on how you want to use middlewares

这篇关于在express.js上使用一系列中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆