如何在节点红色中进行http重定向? [英] How to make http redirect in node red?
问题描述
我需要重定向到另一个网址.我正在使用 nodered 请求一个 url,然后获取用户名和密码,在 cloudant 数据库中检查它们,在确定它们存在之后,我需要重定向到另一个/home.是否有要重定向的节点?我尝试使用 http 请求节点,但它只是以 json 或二进制格式获取响应,而不是重定向 url.如何在 nodered 中重定向?
为了触发重定向,您需要做两件事:
- 将响应的状态代码设置为适当的值
- 将
location
标头设置为要重定向到的网址.
参考HTTP Response
节点的帮助,可以直接在HTTP Response节点设置状态码,也可以在消息上设置msg.statusCode
传递节点.
您设置的值将取决于您要执行的重定向类型 - 无论是永久重定向还是临时重定向.
例如,一种常见的模式是将创建资源的 POST 请求重定向到新资源的 GET 请求.这将是使用 303
作为状态代码的临时重定向.
I need to redirect to another url. I am using nodered to request a url, then getting the username and password, checking them in a cloudant database and after veryfing that they are exist, i need to redirect to another /home. Is there a node to redirect?. I tried with http request node, but it just get the response in json or binary not redirecting the url. How to redirect in nodered?
In order to trigger a redirect you need to do two things:
- set the status code of the response to the appropriate value
- set the
location
header to the url to redirect to.
Referring to the help for the HTTP Response
node, you can set the status code either directly in the HTTP Response node, or by setting msg.statusCode
on the message to pass the node.
The value you set it to will depend on the type of redirection you want to do - whether its a permanent or temporary redirect.
For example, a common pattern is to redirect a POST request that creates a resource to a GET request of the new resource. That would be a temporary redirect using 303
as the status code.
This is a good reference for the different types of redirect code.
To set the Location
header, you can again, either hardcode it in the HTTP Response node, or set it under the msg.headers
property.
For example, a Function node such as the following would do it:
msg.statusCode = 303;
msg.headers = {
Location: "https://google.com"
}
delete msg.payload;
return;
You could also use a Change node to do the same:
这篇关于如何在节点红色中进行http重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!