gpt4 book ai didi

hapijs - 如何在路由中添加中间件?

转载 作者:行者123 更新时间:2023-12-04 01:29:01 27 4
gpt4 key购买 nike

在 express 中,我有这样的事情:

router.get('/foo', middlewareFunction, function (req, res) {
res.send('YoYo');
});

hapi中间件的形式是什么?当我有这个时:
server.route({
method: 'GET',
path: '/foo',
handler: function (request, reply) {
reply('YoYo');
}
})

最佳答案

route pre 选项允许定义这样的预处理器方法,请查看 http://hapijs.com/api#route-prerequisites

const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 80 });

const pre1 = function (request, reply) {

return reply('Hello');
};

const pre2 = function (request, reply) {

return reply('World');
};

const pre3 = function (request, reply) {

return reply(request.pre.m1 + ' ' + request.pre.m2);
};

server.route({
method: 'GET',
path: '/',
config: {
pre: [
[
// m1 and m2 executed in parallel
{ method: pre1, assign: 'm1' },
{ method: pre2, assign: 'm2' }
],
{ method: pre3, assign: 'm3' },
],
handler: function (request, reply) {

return reply(request.pre.m3 + '\n');
}
}
});

关于hapijs - 如何在路由中添加中间件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31331606/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com