From 6b278fdceb6bf4f945a0b8a5a69c9e74021d2da8 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 17 Feb 2014 22:32:53 +0000 Subject: [PATCH] Add httpNodeCors setting Adds a dependency on the 'cors' npm module --- nodes/core/io/21-httpin.js | 40 +++++++++++++++++++++++++++++--------- package.json | 1 + settings.js | 9 +++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/nodes/core/io/21-httpin.js b/nodes/core/io/21-httpin.js index 188510586..557d30b96 100644 --- a/nodes/core/io/21-httpin.js +++ b/nodes/core/io/21-httpin.js @@ -21,6 +21,7 @@ var https = require("follow-redirects").https; var urllib = require("url"); var express = require("express"); var getBody = require('raw-body'); +var cors = require('cors'); var jsonParser = express.json(); var urlencParser = express.urlencoded(); @@ -53,28 +54,49 @@ function HTTPIn(n) { }; this.callback = function(req,res) { - if (node.method == "post") { node.send({req:req,res:res,payload:req.body}); } - else if (node.method == "get") { node.send({req:req,res:res,payload:req.query}); } - else node.send({req:req,res:res}); + if (node.method == "post") { + node.send({req:req,res:res,payload:req.body}); + } else if (node.method == "get") { + node.send({req:req,res:res,payload:req.query}); + } else { + node.send({req:req,res:res}); + } } + + var corsHandler = function(req,res,next) { next(); } + + if (RED.settings.httpNodeCors) { + corsHandler = cors(RED.settings.httpNodeCors); + RED.httpNode.options(this.url,corsHandler); + } + if (this.method == "get") { - RED.httpNode.get(this.url,this.callback,this.errorHandler); + RED.httpNode.get(this.url,corsHandler,this.callback,this.errorHandler); } else if (this.method == "post") { - RED.httpNode.post(this.url,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); + RED.httpNode.post(this.url,corsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "put") { - RED.httpNode.put(this.url,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); + RED.httpNode.put(this.url,corsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "delete") { - RED.httpNode.delete(this.url,this.callback,errorHandler); + RED.httpNode.delete(this.url,corsHandler,this.callback,errorHandler); } this.on("close",function() { - var routes = RED.httpNode.routes[this.method]; - for (var i in routes) { + var routes = RED.httpNode.routes[this.method]; + for (var i = 0; i