• V
 

创建一个 HTTP 端点

问题

您想创建一个 HTTP 端点,响应 GET 请求,并返回一些静态内容,例如 HTML 页面或 CSS 样式表。

解决方案

使用 HTTP In 节点监听请求,使用 Template 节点包含静态内容,并使用 HTTP Response 节点回复请求。

示例

[{"id":"59ff2a1.fa600d4","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello","method":"get","swaggerDoc":"","x":100,"y":80,"wires":[["54c1e70d.ab3e18"]]},{"id":"54c1e70d.ab3e18","type":"template","z":"3045204d.cfbae","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <head></head>\n    <body>\n        <h1>Hello World!</h1>\n    </body>\n</html>","x":250,"y":80,"wires":[["266c286f.d993d8"]]},{"id":"266c286f.d993d8","type":"http response","z":"3045204d.cfbae","name":"","x":390,"y":80,"wires":[]}]
[~]$ curl http://localhost:1880/hello
<html>
    <head></head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

讨论

HTTP InHTTP Response 节点对是您创建的所有 HTTP 端点的起点。

任何以 HTTP In 节点开头的流程必须有一个指向 HTTP Response 节点的路径,否则请求最终将超时。

HTTP Response 节点使用它接收到的消息的 payload 属性作为响应的主体。可以使用其他属性进一步自定义响应——它们在其他食谱中进行了说明。

Template 节点提供了一种方便的方法,将内容主体嵌入流程中。维护这样的静态内容在流程之外是很有意义的。

如果您开启了 HTTP 身份验证,您可能需要在 curl 命令中添加您的用户 ID 和密码。例如:

[~]$ curl -u userid:password  http://localhost:1880/hello