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>
</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>.
</p>
<p>
<b>Note: </b>This node does not send any response to the http request. This must be done within
a subsequent Function node. The <a href="http://expressjs.com/api.html#res">Express response documentation</a>
describes how this should be done.
<pre>msg.res.send(200, 'Thanks for the request ');<br/>return msg;</pre>
<b>Note: </b>This node does not send any response to the http request. This should be done with
a subsequent HTTP Response node, or Function node.
In the case of a Function node, the <a href="http://expressjs.com/api.html#res">Express response documentation</a>
describes how this should be done. For example:
<pre>msg.res.send(200, 'Thanks for the request ');<br/>return msg;</pre>
</p>
</script>
@ -81,5 +82,37 @@
return this.name?"node_label_italic":"";
}
});
</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") {
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);
HTTPIn.prototype.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;
}
}
function HTTPOut(n) {
RED.nodes.createNode(this,n);
this.on("input",function(msg) {
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);