我使用了express,在那里您可以通过多个方法来传递路由,如下所示:
app.get('/users/,[
validator.validate,
controller.get
]);然后,每个函数都使用 next ()回调将控件传递给数组中的下一个函数。在hapijs处理程序中是否有类似的操作?我希望我的函数既可重用又独立,就像我们对于express路由处理程序一样。
谢谢。
发布于 2015-06-30 10:39:41
hapi有路由先决条件,它允许在实际处理程序本身之前运行一组类似于处理程序的函数。它们都是可重用的和独立的,只要您在配置本身之外定义它们。
在request.pre对象上每个pre的集合中生成的值,以便在处理程序中使用。下面是一个例子:
var step1 = function (request, reply) {
reply('The quick brown fox ');
};
var step2 = function (request, reply) {
reply('jumped over the lazy dog.');
};
server.route({
config: {
pre: [
step1,
step2
]
},
method: 'GET',
path: '/',
handler: function (request, reply) {
var response = request.pre.step1 + request.pre.step2;
reply(response);
}
});默认情况下,每个pre都将以串联方式运行,类似于async.series/waterfall包中的异步函数。如果您希望一组pres并行运行,只需将它们放在一个数组中,您就会得到类似于async.parallel的内容
server.route({
...
config: {
pre: [
[ step1, step2 ], // these run together
step3 // and then this one
]
},
...
});https://stackoverflow.com/questions/31130095
复制相似问题