gpt4 book ai didi

javascript - 向 Node.js 上的 azure 函数添加中间件函数

转载 作者:行者123 更新时间:2023-12-02 23:40:35 25 4
gpt4 key购买 nike

我正在尝试复制您在 Azure 函数上的 Express 中可能拥有的中间件。

例如:

router.get('/protectedEndpoint', secured(), function (req, res) {

其中 secure() 函数是一个中间件,如果有效,它将发送 next() 。

azure 的问题是它是按照这种风格完成的

module.exports = function (context) {

我不确定如何在其中使用 next() 运行中间件

这是该函数的一个愚蠢示例:

module.exports = function () {
return function secured (req, res, next) {
if (req.user) { return next(); }
req.session.returnTo = req.originalUrl;
res.redirect('/login');
};
};

最佳答案

借助azure功能,您可以使用azure-middleware引擎添加中间件,就像使用Express一样。通过这种方法,您可以执行与使用 Express 相同的操作。该引擎的链接如下azure-middlware

var MiddlewareHandler =  require('azure-middleware')

module.exports = new MiddlewareHandler()
.use((ctx) => {
secured();//Your middleware function
ctx.next();
})
.use(async (ctx) => {
//Your controller or your main function script
ctx.log.info('Im called third');
ctx.done(null, { status: 200 });
})
.catch((error, ctx, msg) => {
ctx.log.info(error); // ERROR!
ctx.next();
})
.listen();

关于javascript - 向 Node.js 上的 azure 函数添加中间件函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62439308/

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