1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add HTTP Response node to make http flows easier to handle

This commit is contained in:
Nicholas O'Leary 2013-09-28 22:18:41 +01:00
parent 6341c2e907
commit 68e7e5e226
2 changed files with 63 additions and 14 deletions

View File

@ -50,14 +50,15 @@
<pre>foo=bar&amp;this=that</pre> <pre>foo=bar&amp;this=that</pre>
</p> </p>
<p> <p>
To send JSON encoded data, the content-type header of the request must be set to To send JSON encoded data to the node, the content-type header of the request must be set to
<code>application/json</code>. <code>application/json</code>.
</p> </p>
<p> <p>
<b>Note: </b>This node does not send any response to the http request. This must be done within <b>Note: </b>This node does not send any response to the http request. This should be done with
a subsequent Function node. The <a href="http://expressjs.com/api.html#res">Express response documentation</a> a subsequent HTTP Response node, or Function node.
describes how this should be done. In the case of a Function node, the <a href="http://expressjs.com/api.html#res">Express response documentation</a>
<pre>msg.res.send(200, 'Thanks for the request ');<br/>return msg;</pre> describes how this should be done. For example:
<pre>msg.res.send(200, 'Thanks for the request ');<br/>return msg;</pre>
</p> </p>
</script> </script>
@ -81,5 +82,37 @@
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
} }
}); });
</script> </script>
<script type="text/x-red" data-template-name="http response">
<div class="form-tips">The messages sent to this node must originate from an HTTP input node</div>
</script>
<script type="text/x-red" data-help-name="http response">
<p>Provides an response node for http requests received from an http input node.</p>
<ul>
<li><code>payload</code> is sent as the body of the reponse</li>
<li><code>rc</code>, if set, is used as the response code (default: 200)</li>
<li><code>headers</code>, if set, should be an object containing field/value
pairs to be added as response headers.</li>
</ul>
</script>
<script type="text/javascript">
RED.nodes.registerType('http response',{
category: 'output',
color:"rgb(231, 231, 174)",
defaults: {},
inputs:1,
outputs:0,
align: "right",
icon: "white-globe.png",
label: function() {
return "http"
},
labelStyle: function() {
return "";
}
});
</script>

View File

@ -35,17 +35,33 @@ function HTTPIn(n) {
} else if (this.method == "delete") { } else if (this.method == "delete") {
RED.app.delete(this.url,this.callback); RED.app.delete(this.url,this.callback);
} }
this.on("close",function() {
var routes = RED.app.routes[this.method];
for (var i in routes) {
if (routes[i].path == this.url) {
routes.splice(i,1);
break;
}
}
});
} }
RED.nodes.registerType("http in",HTTPIn); RED.nodes.registerType("http in",HTTPIn);
HTTPIn.prototype.close = function() {
var routes = RED.app.routes[this.method]; function HTTPOut(n) {
for (var i in routes) { RED.nodes.createNode(this,n);
if (routes[i].path == this.url) {
routes.splice(i,1); this.on("input",function(msg) {
break; if (msg.res) {
} if (msg.headers) {
} res.set(msg.headers);
}
var rc = msg.rc || 200;
msg.res.send(rc,msg.payload);
}
});
} }
RED.nodes.registerType("http response",HTTPOut);