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

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

问题描述

我希望我的Azure功能之一执行 HTTP重定向.

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

这是该函数的当前代码:

This is the current code of the function:

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

但是它不起作用.

邮递员发送的请求显示响应如下:

A request sent from Postman shows the response has:

  • Status:200
  • Location未设置
  • 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);
};

或使用表达风格" 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天全站免登陆