我希望我的 Azure 函数之一执行 HTTP 重定向。
这是该函数的当前代码:
module.exports = context => {
context.res.status(302)
context.res.header('Location', 'https://www.stackoverflow.com')
context.done()
}
但是它不起作用。
从 Postman 发送的请求显示响应具有:
状态
:200位置
未设置
这个代码正确吗?或者 Azure Functions 根本不允许这样做?
最佳答案
上面的代码实际上确实有效,除非您将绑定(bind)名称设置为 $return,这就是我假设您现在拥有的(您可以在集成选项卡中查看)
以下任一选项也可以满足您的要求
假设绑定(bind)配置中有$return:
module.exports = function (context, req) {
var res = { status: 302, headers: { "location": "https://www.stackoverflow.com" }, body: null};
context.done(null, res);
};
或者使用“express style”API(不在绑定(bind)配置中使用 $return):
module.exports = function (context, req) {
context.res.status(302)
.set('location','https://www.stackoverflow.com')
.send();
};
关于javascript - Azure Functions 重定向 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42931965/