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

Add httpNodeCors setting

Adds a dependency on the 'cors' npm module
This commit is contained in:
Nick O'Leary 2014-02-17 22:32:53 +00:00
parent 09f162d933
commit 6b278fdceb
3 changed files with 41 additions and 9 deletions

View File

@ -21,6 +21,7 @@ var https = require("follow-redirects").https;
var urllib = require("url"); var urllib = require("url");
var express = require("express"); var express = require("express");
var getBody = require('raw-body'); var getBody = require('raw-body');
var cors = require('cors');
var jsonParser = express.json(); var jsonParser = express.json();
var urlencParser = express.urlencoded(); var urlencParser = express.urlencoded();
@ -53,28 +54,49 @@ function HTTPIn(n) {
}; };
this.callback = function(req,res) { this.callback = function(req,res) {
if (node.method == "post") { node.send({req:req,res:res,payload:req.body}); } if (node.method == "post") {
else if (node.method == "get") { node.send({req:req,res:res,payload:req.query}); } node.send({req:req,res:res,payload:req.body});
else node.send({req:req,res:res}); } 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") { 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") { } 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") { } 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") { } 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() { this.on("close",function() {
var routes = RED.httpNode.routes[this.method]; var routes = RED.httpNode.routes[this.method];
for (var i in routes) { for (var i = 0; i<routes.length; i++) {
if (routes[i].path == this.url) {
routes.splice(i,1);
//break;
}
}
if (RED.settings.httpNodeCors) {
var routes = RED.httpNode.routes['options'];
for (var i = 0; i<routes.length; i++) {
if (routes[i].path == this.url) { if (routes[i].path == this.url) {
routes.splice(i,1); routes.splice(i,1);
//break; //break;
} }
} }
}
}); });
} }

View File

@ -36,6 +36,7 @@
"sentiment":"~0.2.1", "sentiment":"~0.2.1",
"irc":"~0.3.6", "irc":"~0.3.6",
"follow-redirects":"~0.0.3", "follow-redirects":"~0.0.3",
"cors":"~2.1.1",
"mkdirp":"~0.3.5" "mkdirp":"~0.3.5"
}, },
"devDependencies": { "devDependencies": {

View File

@ -92,6 +92,15 @@ module.exports = {
// cert: fs.readFileSync('certificate.pem') // cert: fs.readFileSync('certificate.pem')
//}, //},
// The following property can be used to configure cross-origin resource sharing
// in the HTTP nodes.
// See https://github.com/troygoode/node-cors#configuration-options for
// details on its contents. The following is a basic permissive set of options:
//httpNodeCors: {
// origin: "*",
// methods: "GET,PUT,POST,DELETE"
//},
// Anything in this hash is globally available to all functions. // Anything in this hash is globally available to all functions.
// It is accessed as context.global. // It is accessed as context.global.
// eg: // eg: