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

Move to express 4.x

This commit is contained in:
Nick O'Leary 2015-07-15 22:43:24 +01:00
parent ca91a5dd95
commit d668d43a0a
18 changed files with 143 additions and 148 deletions

View File

@ -81,13 +81,13 @@ module.exports = function(RED) {
if (node != null) {
try {
node.receive();
res.send(200);
res.sendStatus(200);
} catch(err) {
res.send(500);
res.sendStatus(500);
node.error(RED._("inject.failed",{error:err.toString()}));
}
} else {
res.send(404);
res.sendStatus(404);
}
});
}

View File

@ -142,15 +142,15 @@ module.exports = function(RED) {
if (node !== null && typeof node !== "undefined" ) {
if (state === "enable") {
node.active = true;
res.send(200);
res.sendStatus(200);
} else if (state === "disable") {
node.active = false;
res.send(201);
res.sendStatus(201);
} else {
res.send(404);
res.sendStatus(404);
}
} else {
res.send(404);
res.sendStatus(404);
}
});
};

View File

@ -19,13 +19,13 @@ module.exports = function(RED) {
var http = require("follow-redirects").http;
var https = require("follow-redirects").https;
var urllib = require("url");
var express = require("express");
var bodyParser = require("body-parser");
var getBody = require('raw-body');
var mustache = require("mustache");
var querystring = require("querystring");
var cors = require('cors');
var jsonParser = express.json();
var urlencParser = express.urlencoded();
var jsonParser = bodyParser.json();
var urlencParser = bodyParser.urlencoded({extended:true});
var onHeaders = require('on-headers');
function rawBodyParser(req, res, next) {
@ -43,6 +43,7 @@ module.exports = function(RED) {
});
}
var corsSetup = false;
function HTTPIn(n) {
RED.nodes.createNode(this,n);
@ -60,7 +61,7 @@ module.exports = function(RED) {
this.errorHandler = function(err,req,res,next) {
node.warn(err);
res.send(500);
res.sendStatus(500);
};
this.callback = function(req,res) {
@ -77,9 +78,10 @@ module.exports = function(RED) {
var corsHandler = function(req,res,next) { next(); }
if (RED.settings.httpNodeCors) {
if (RED.settings.httpNodeCors && !corsSetup) {
corsHandler = cors(RED.settings.httpNodeCors);
RED.httpNode.options(this.url,corsHandler);
RED.httpNode.options("*",corsHandler);
corsSetup = true;
}
var httpMiddleware = function(req,res,next) { next(); }
@ -121,25 +123,13 @@ module.exports = function(RED) {
}
this.on("close",function() {
var routes = RED.httpNode.routes[this.method];
for (var i = 0; i<routes.length; i++) {
if (routes[i].path == this.url) {
var node = this;
RED.httpNode._router.stack.forEach(function(route,i,routes) {
if (route.route && route.route.path === node.url && route.route.methods[node.method]) {
routes.splice(i,1);
//break;
}
}
if (RED.settings.httpNodeCors) {
var routes = RED.httpNode.routes['options'];
if (routes) {
for (var j = 0; j<routes.length; j++) {
if (routes[j].path == this.url) {
routes.splice(j,1);
//break;
}
}
}
}
});
});
} else {
this.warn(RED._("httpin.errors.not-created"));
}
@ -157,7 +147,7 @@ module.exports = function(RED) {
}
var statusCode = msg.statusCode || 200;
if (typeof msg.payload == "object" && !Buffer.isBuffer(msg.payload)) {
msg.res.jsonp(statusCode,msg.payload);
msg.res.status(statusCode).jsonp(msg.payload);
} else {
if (msg.res.get('content-length') == null) {
var len;

View File

@ -25,7 +25,8 @@
"editor", "messaging", "iot", "m2m", "pi", "arduino", "beaglebone", "ibm", "flow"
],
"dependencies": {
"express": "3.20.3",
"express": "4.13.1",
"body-parser": "1.13.2",
"when": "3.7.3",
"bcryptjs": "2.2.0",
"nopt": "3.0.3",

View File

@ -55,7 +55,7 @@ function needsPermission(permission) {
return next();
}
log.audit({event: "permission.fail"},req);
return res.send(401);
return res.status(401).end();
});
} else {
next();
@ -95,7 +95,7 @@ function revoke(req,res) {
// TODO: audit log
Tokens.revoke(token).then(function() {
log.audit({event: "auth.login.revoke"},req);
res.send(200);
res.status(200).end();
});
}

View File

@ -29,11 +29,11 @@ module.exports = {
var deploymentType = req.get("Node-RED-Deployment-Type")||"full";
log.audit({event: "flows.set",type:deploymentType},req);
redNodes.setFlows(flows,deploymentType).then(function() {
res.send(204);
res.status(204).end();
}).otherwise(function(err) {
log.warn(log._("api.flows.error-save",{message:err.message}));
log.warn(err.stack);
res.json(500,{error:"unexpected_error", message:err.message});
res.status(500).json({error:"unexpected_error", message:err.message});
});
}
}

View File

@ -15,6 +15,7 @@
**/
var express = require("express");
var bodyParser = require("body-parser");
var util = require('util');
var path = require('path');
var passport = require('passport');
@ -34,7 +35,7 @@ var settings = require("../settings");
var errorHandler = function(err,req,res,next) {
console.log(err.stack);
res.json(400,{error:"unexpected_error", message:err.toString()});
res.status(400).json({error:"unexpected_error", message:err.toString()});
};
function init(adminApp,storage) {
@ -54,8 +55,8 @@ function init(adminApp,storage) {
adminApp.use(editorApp);
}
adminApp.use(express.json());
adminApp.use(express.urlencoded());
adminApp.use(bodyParser.json());
adminApp.use(bodyParser.urlencoded({extended:true}));
adminApp.get("/auth/login",auth.login);

View File

@ -38,12 +38,12 @@ function createLibrary(type) {
log.warn(log._("api.library.error-load-entry",{path:path,message:err.toString()}));
if (err.code === 'forbidden') {
log.audit({event: "library.get",type:type,error:"forbidden"},req);
res.send(403);
res.status(403).end();
return;
}
}
log.audit({event: "library.get",type:type,error:"not_found"},req);
res.send(404);
res.status(404).end();
});
});
@ -55,16 +55,16 @@ function createLibrary(type) {
storage.saveLibraryEntry(type,path,meta,text).then(function() {
log.audit({event: "library.set",type:type},req);
res.send(204);
res.status(204).end();
}).otherwise(function(err) {
log.warn(log._("api.library.error-save-entry",{path:path,message:err.toString()}));
if (err.code === 'forbidden') {
log.audit({event: "library.set",type:type,error:"forbidden"},req);
res.send(403);
res.status(403).end();
return;
}
log.audit({event: "library.set",type:type,error:"unexpected_error",message:err.toString()},req);
res.json(500,{error:"unexpected_error", message:err.toString()});
res.status(500).json({error:"unexpected_error", message:err.toString()});
});
});
}
@ -92,28 +92,28 @@ module.exports = {
log.warn(log._("api.library.error-load-flow",{path:req.params[0],message:err.toString()}));
if (err.code === 'forbidden') {
log.audit({event: "library.get",type:"flow",path:req.params[0],error:"forbidden"},req);
res.send(403);
res.status(403).end();
return;
}
}
log.audit({event: "library.get",type:"flow",path:req.params[0],error:"not_found"},req);
res.send(404);
res.status(404).end();
});
},
post: function(req,res) {
var flow = JSON.stringify(req.body);
storage.saveFlow(req.params[0],flow).then(function() {
log.audit({event: "library.set",type:"flow",path:req.params[0]},req);
res.send(204);
res.status(204).end();
}).otherwise(function(err) {
log.warn(log._("api.library.error-save-flow",{path:req.params[0],message:err.toString()}));
if (err.code === 'forbidden') {
log.audit({event: "library.set",type:"flow",path:req.params[0],error:"forbidden"},req);
res.send(403);
res.status(403).end();
return;
}
log.audit({event: "library.set",type:"flow",path:req.params[0],error:"unexpected_error",message:err.toString()},req);
res.send(500,{error:"unexpected_error", message:err.toString()});
res.status(500).send({error:"unexpected_error", message:err.toString()});
});
}
}

View File

@ -19,7 +19,7 @@ module.exports = {
get: function(req,res) {
var namespace = req.params[0];
namespace = namespace.replace(/\.json$/,"");
var lang = i18n.determineLangFromHeaders(req.acceptedLanguages || []);
var lang = i18n.determineLangFromHeaders(req.acceptsLanguages() || []);
var prevLang = i18n.i.lng();
i18n.i.setLng(lang, function(){
var catalog = i18n.catalog(namespace,lang);

View File

@ -29,7 +29,7 @@ module.exports = {
log.audit({event: "nodes.list.get"},req);
res.json(redNodes.getNodeList());
} else {
var lang = i18n.determineLangFromHeaders(req.acceptedLanguages);
var lang = i18n.determineLangFromHeaders(req.acceptsLanguages());
log.audit({event: "nodes.configs.get"},req);
res.send(redNodes.getNodeConfigs(lang));
}
@ -38,7 +38,7 @@ module.exports = {
post: function(req,res) {
if (!settings.available()) {
log.audit({event: "nodes.install",error:"settings_unavailable"},req);
res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
return;
}
var node = req.body;
@ -47,7 +47,7 @@ module.exports = {
var module = redNodes.getModuleInfo(node.module);
if (module) {
log.audit({event: "nodes.install",module:node.module,error:"module_already_loaded"},req);
res.json(400,{error:"module_already_loaded", message:"Module already loaded"});
res.status(400).json({error:"module_already_loaded", message:"Module already loaded"});
return;
}
promise = server.installModule(node.module);
@ -55,7 +55,7 @@ module.exports = {
promise = server.installNode(node.file);
} else {
log.audit({event: "nodes.install",module:node.module,error:"invalid_request"},req);
res.json(400,{error:"invalid_request", message:"Invalid request"});
res.status(400).json({error:"invalid_request", message:"Invalid request"});
return;
}
promise.then(function(info) {
@ -69,13 +69,13 @@ module.exports = {
}).otherwise(function(err) {
if (err.code === 404) {
log.audit({event: "nodes.install",module:node.module,file:node.file,error:"not_found"},req);
res.send(404);
res.status(404).end();
} else if (err.code) {
log.audit({event: "nodes.install",module:node.module,error:err.code},req);
res.json(400,{error:err.code, message:err.message});
res.status(400).json({error:err.code, message:err.message});
} else {
log.audit({event: "nodes.install",module:node.module,file:node.file,error:err.code||"unexpected_error",message:err.toString()},req);
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
}
});
},
@ -83,7 +83,7 @@ module.exports = {
delete: function(req,res) {
if (!settings.available()) {
log.audit({event: "nodes.remove",error:"settings_unavailable"},req);
res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
return;
}
var mod = req.params.mod;
@ -92,7 +92,7 @@ module.exports = {
var module = redNodes.getModuleInfo(mod);
if (!module) {
log.audit({event: "nodes.remove",module:mod,error:"not_found"},req);
res.send(404);
res.status(404).end();
return;
} else {
promise = server.uninstallModule(mod);
@ -100,14 +100,14 @@ module.exports = {
promise.then(function() {
log.audit({event: "nodes.remove",module:mod},req);
res.send(204);
res.status(204).end();
}).otherwise(function(err) {
log.audit({event: "nodes.remove",module:mod,error:err.code||"unexpected_error",message:err.toString()},req);
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
});
} catch(err) {
log.audit({event: "nodes.remove",module:mod,error:err.code||"unexpected_error",message:err.toString()},req);
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
}
},
@ -122,17 +122,17 @@ module.exports = {
res.send(result);
} else {
log.audit({event: "nodes.info.get",id:id,error:"not_found"},req);
res.send(404);
res.status(404).end();
}
} else {
var lang = i18n.determineLangFromHeaders(req.acceptedLanguages);
var lang = i18n.determineLangFromHeaders(req.acceptsLanguages());
result = redNodes.getNodeConfig(id,lang);
if (result) {
log.audit({event: "nodes.config.get",id:id},req);
res.send(result);
} else {
log.audit({event: "nodes.config.get",id:id,error:"not_found"},req);
res.send(404);
res.status(404).end();
}
}
},
@ -145,20 +145,20 @@ module.exports = {
res.json(result);
} else {
log.audit({event: "nodes.module.get",module:module,error:"not_found"},req);
res.send(404);
res.status(404).end();
}
},
putSet: function(req,res) {
if (!settings.available()) {
log.audit({event: "nodes.info.set",error:"settings_unavailable"},req);
res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
return;
}
var body = req.body;
if (!body.hasOwnProperty("enabled")) {
log.audit({event: "nodes.info.set",error:"invalid_request"},req);
res.json(400,{error:"invalid_request", message:"Invalid request"});
res.status(400).json({error:"invalid_request", message:"Invalid request"});
return;
}
try {
@ -167,7 +167,7 @@ module.exports = {
var info;
if (!node) {
log.audit({event: "nodes.info.set",id:id,error:"not_found"},req);
res.send(404);
res.status(404).end();
} else {
delete node.loaded;
putNode(node, body.enabled).then(function(result) {
@ -177,20 +177,20 @@ module.exports = {
}
} catch(err) {
log.audit({event: "nodes.info.set",id:id,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
}
},
putModule: function(req,res) {
if (!settings.available()) {
log.audit({event: "nodes.module.set",error:"settings_unavailable"},req);
res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
return;
}
var body = req.body;
if (!body.hasOwnProperty("enabled")) {
log.audit({event: "nodes.module.set",error:"invalid_request"},req);
res.json(400,{error:"invalid_request", message:"Invalid request"});
res.status(400).json({error:"invalid_request", message:"Invalid request"});
return;
}
try {
@ -198,7 +198,7 @@ module.exports = {
var module = redNodes.getModuleInfo(mod);
if (!module) {
log.audit({event: "nodes.module.set",module:mod,error:"not_found"},req);
return res.send(404);
return res.status(404).end();
}
var nodes = module.nodes;
@ -211,7 +211,7 @@ module.exports = {
});
} catch(err) {
log.audit({event: "nodes.module.set",module:mod,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
}
}
};

View File

@ -43,7 +43,7 @@ function serveFile(app,baseUrl,file) {
var url = baseUrl+path.basename(file);
//console.log(url,"->",file);
app.get(url,function(req, res) {
res.sendfile(file);
res.sendFile(file);
});
return "theme"+url;
} catch(err) {

View File

@ -55,17 +55,17 @@ module.exports = {
},
icon: function(req,res) {
if (iconCache[req.params.icon]) {
res.sendfile(iconCache[req.params.icon]); // if not found, express prints this to the console and serves 404
res.sendFile(iconCache[req.params.icon]); // if not found, express prints this to the console and serves 404
} else {
for (var p=0;p<icon_paths.length;p++) {
var iconPath = path.join(icon_paths[p],req.params.icon);
if (fs.existsSync(iconPath)) {
res.sendfile(iconPath);
res.sendFile(iconPath);
iconCache[req.params.icon] = iconPath;
return;
}
}
res.sendfile(defaultIcon);
res.sendFile(defaultIcon);
}
},
editor: function(req,res) {

View File

@ -20,7 +20,7 @@ var path = require("path");
var fs = require("fs");
var defaultLang = "en-US";
var supportedLangs = null;
var supportedLangs = [];
var resourceMap = {
"runtime": {

View File

@ -60,7 +60,7 @@ describe("api auth middleware",function() {
var req = { body: { token: "abcdef" } };
var res = { send: function(resp) {
var res = { status: function(resp) {
revokeToken.restore();
resp.should.equal(200);

View File

@ -17,6 +17,7 @@
var should = require("should");
var request = require('supertest');
var express = require('express');
var bodyParser = require('body-parser');
var sinon = require('sinon');
var when = require('when');
@ -30,7 +31,7 @@ describe("flows api", function() {
before(function() {
app = express();
app.use(express.json());
app.use(bodyParser.json());
app.get("/flows",flows.get);
app.post("/flows",flows.post);
});

View File

@ -17,6 +17,7 @@
var should = require("should");
var request = require('supertest');
var express = require('express');
var bodyParser = require('body-parser');
var when = require('when');
@ -70,7 +71,7 @@ describe("library api", function() {
before(function() {
app = express();
app.use(express.json());
app.use(bodyParser.json());
app.get("/library/flows",library.getAll);
app.post(new RegExp("/library/flows\/(.*)"),library.post);
app.get(new RegExp("/library/flows\/(.*)"),library.get);
@ -165,7 +166,7 @@ describe("library api", function() {
before(function() {
app = express();
app.use(express.json());
app.use(bodyParser.json());
library.init(app);
auth.init({});
RED.library.register("test");

View File

@ -17,6 +17,7 @@
var should = require("should");
var request = require('supertest');
var express = require('express');
var bodyParser = require('body-parser');
var sinon = require('sinon');
var when = require('when');
@ -32,7 +33,7 @@ describe("nodes api", function() {
before(function() {
app = express();
app.use(express.json());
app.use(bodyParser.json());
app.get("/nodes",nodes.getAll);
app.post("/nodes",nodes.post);
app.get("/nodes/:mod",nodes.getModule);

View File

@ -31,7 +31,7 @@ describe("ui api", function() {
describe("slash handler", function() {
before(function() {
app = express();
app.get("/foo",ui.ensureSlash,function(req,res) { res.send(200);});
app.get("/foo",ui.ensureSlash,function(req,res) { res.sendStatus(200);});
});
it('redirects if the path does not end in a slash',function(done) {
request(app)