Azure Functions 重定向标头 [英] Azure Functions Redirect Header

查看:28
本文介绍了Azure Functions 重定向标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的一个 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();
};

这篇关于Azure Functions 重定向标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆