• 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 https://: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 身份验证,则可能需要将您的用户 ID 和密码添加到 curl 命令中,例如:

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