Azure Functions 重定向标头

2022-01-17 00:00:00 azure azure-functions javascript

我希望我的一个 Azure Functions 执行 HTTP 重定向.

I want one of my Azure Functions to do an HTTP Redirection.

这是函数的当前代码:

module.exports = context => {
  context.res.status(302)
  context.res.header('Location', 'https://www.stackoverflow.com')
  context.done()
}

但它不起作用.

从 Postman 发送的请求显示响应有:

A request sent from Postman shows the response has:

  • 状态:200
  • 位置未设置
  • Status: 200
  • Location not set

这是正确的代码吗?还是 Azure Functions 根本不允许?

Is this correct code? Or is it simply not allowed by Azure Functions?

推荐答案

上面的代码确实有效,除非您将绑定名称设置为 $return,这就是我假设您现在拥有的(您可以在集成标签)

The code above actually does work, unless you have your binding name set to $return, which is what I assume you have now (you can check in the integrate tab)

以下任一选项也可以满足您的需求

Either of the following options will also do what you're looking for

假设 $return 在绑定配置中:

Assuming $return in the binding configuration:

module.exports = function (context, req) {
var res = { status: 302, headers: { "location": "https://www.stackoverflow.com" }, body: null};
context.done(null, res);
};

或者使用express style"API(在绑定配置中不使用$return):

Or using the "express style" API (not using $return in the binding configuration):

module.exports = function (context, req) {
context.res.status(302)
            .set('location','https://www.stackoverflow.com')
            .send();
};

相关文章