From b10141d71fea411bf35a19df4c438e1532bdc8bb Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 24 Jan 2017 14:56:48 +0000 Subject: [PATCH] Allow statusCode/headers to be set directly within HTTP Response node --- nodes/core/io/21-httpin.html | 149 ++++++++++++++++++++++++- nodes/core/io/21-httpin.js | 14 ++- nodes/core/locales/en-US/messages.json | 4 +- 3 files changed, 162 insertions(+), 5 deletions(-) diff --git a/nodes/core/io/21-httpin.html b/nodes/core/io/21-httpin.html index 4802314bc..a4cac1f9f 100644 --- a/nodes/core/io/21-httpin.html +++ b/nodes/core/io/21-httpin.html @@ -79,6 +79,16 @@ +
+ + +
+
+ +
+
+
    +
    @@ -92,6 +102,9 @@ pairs to be added as response headers.
  1. cookies, if set, can be used to set or delete cookies. +

    The statusCode and headers can also be set within + the node itself. If a property is set within the node, it cannot be overridden + by the corresponding message property.

    Cookie handling

    The cookies property must be an object of name/value pairs. The value can be either a string to set the value of the cookie with default @@ -120,6 +133,7 @@ msg.cookies = { diff --git a/nodes/core/io/21-httpin.js b/nodes/core/io/21-httpin.js index 418dc7444..57c8f734a 100644 --- a/nodes/core/io/21-httpin.js +++ b/nodes/core/io/21-httpin.js @@ -268,10 +268,20 @@ module.exports = function(RED) { function HTTPOut(n) { RED.nodes.createNode(this,n); var node = this; + this.headers = n.headers||{}; + this.statusCode = n.statusCode; this.on("input",function(msg) { if (msg.res) { + var headers = RED.util.cloneMessage(node.headers); if (msg.headers) { - msg.res._res.set(msg.headers); + for (var h in msg.headers) { + if (msg.headers.hasOwnProperty(h) && !headers.hasOwnProperty(h)) { + headers[h] = msg.headers[h]; + } + } + } + if (Object.keys(headers).length > 0) { + msg.res._res.set(headers); } if (msg.cookies) { for (var name in msg.cookies) { @@ -290,7 +300,7 @@ module.exports = function(RED) { } } } - var statusCode = msg.statusCode || 200; + var statusCode = node.statusCode || msg.statusCode || 200; if (typeof msg.payload == "object" && !Buffer.isBuffer(msg.payload)) { msg.res._res.status(statusCode).jsonp(msg.payload); } else { diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index f6cb9113a..7a13c2dd8 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -314,7 +314,9 @@ "url": "URL", "doc": "Docs", "return": "Return", - "upload": "Accept file uploads?" + "upload": "Accept file uploads?", + "status": "Status code", + "headers": "Headers" }, "setby": "- set by msg.method -", "basicauth": "Use basic authentication",