Skip to content

Commit

Permalink
Move to express 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Jul 15, 2015
1 parent 5462e25 commit 3b29b36
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 151 deletions.
6 changes: 3 additions & 3 deletions nodes/core/core/20-inject.js
Expand Up @@ -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);
}
});
}
8 changes: 4 additions & 4 deletions nodes/core/core/58-debug.js
Expand Up @@ -136,15 +136,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);
}
});
};
36 changes: 13 additions & 23 deletions nodes/core/io/21-httpin.js
Expand Up @@ -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) {
Expand All @@ -43,6 +43,7 @@ module.exports = function(RED) {
});
}

var corsSetup = false;

function HTTPIn(n) {
RED.nodes.createNode(this,n);
Expand All @@ -56,7 +57,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) {
Expand All @@ -73,9 +74,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(); }
Expand Down Expand Up @@ -117,24 +119,12 @@ 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"));
Expand All @@ -153,7 +143,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;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions red/api/auth/index.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
}

Expand Down
4 changes: 2 additions & 2 deletions red/api/flows.js
Expand Up @@ -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});
});
}
}
7 changes: 4 additions & 3 deletions red/api/index.js
Expand Up @@ -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');
Expand All @@ -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) {
Expand All @@ -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);

Expand Down
28 changes: 14 additions & 14 deletions red/api/library.js
Expand Up @@ -29,7 +29,7 @@ function createLibrary(type) {
if (typeof result === "string") {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(result);
res.end();
res.end();
} else {
res.json(result);
}
Expand All @@ -38,33 +38,33 @@ 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();
});
});

redApp.post(new RegExp("/library/"+type+"\/(.*)"),needsPermission("library.write"),function(req,res) {
var path = req.params[0];
var meta = req.body;
var text = meta.text;
delete meta.text;

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()});
});
});
}
Expand All @@ -74,7 +74,7 @@ module.exports = {
redApp = app;
},
register: createLibrary,

getAll: function(req,res) {
storage.getAllFlows().then(function(flows) {
log.audit({event: "library.get.all",type:"flow"},req);
Expand All @@ -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()});
});
}
}
2 changes: 1 addition & 1 deletion red/api/locales.js
Expand Up @@ -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);
Expand Down

0 comments on commit 3b29b36

Please sign in to comment.