diff --git a/public/red/main.js b/public/red/main.js index 815b07963..bbe92cce5 100644 --- a/public/red/main.js +++ b/public/red/main.js @@ -80,30 +80,35 @@ var RED = function() { $("#btn-icn-deploy").addClass('spinner'); RED.view.dirty(false); - d3.xhr("flows").header("Content-type", "application/json") - .post(JSON.stringify(nns),function(err,resp) { - $("#btn-icn-deploy").removeClass('spinner'); - $("#btn-icn-deploy").addClass('icon-upload'); - if (resp && resp.status == 204) { - RED.notify("Successfully deployed","success"); - RED.nodes.eachNode(function(node) { - if (node.changed) { - node.dirty = true; - node.changed = false; - } - }); - // Once deployed, cannot undo back to a clean state - RED.history.markAllDirty(); - RED.view.redraw(); - } else { - RED.view.dirty(true); - if (resp) { - RED.notify("Error: "+resp,"error"); - } else { - RED.notify("Error: no response from server","error"); - } - console.log(err,resp); + $.ajax({ + url:"flows", + type: "POST", + data: JSON.stringify(nns), + contentType: "application/json; charset=utf-8" + }).done(function(data,textStatus,xhr) { + RED.notify("Successfully deployed","success"); + RED.nodes.eachNode(function(node) { + if (node.changed) { + node.dirty = true; + node.changed = false; } + }); + // Once deployed, cannot undo back to a clean state + RED.history.markAllDirty(); + RED.view.redraw(); + }).fail(function(xhr,textStatus,err) { + RED.view.dirty(true); + console.log(xhr); + console.log(textStatus); + console.log(err); + if (xhr.responseText) { + RED.notify("Error: "+xhr.responseText,"error"); + } else { + RED.notify("Error: no response from server","error"); + } + }).always(function() { + $("#btn-icn-deploy").removeClass('spinner'); + $("#btn-icn-deploy").addClass('icon-upload'); }); } } @@ -150,7 +155,7 @@ var RED = function() { } function loadFlows() { - d3.json("flows",function(nodes) { + $.getJSON("flows",function(nodes) { RED.nodes.import(nodes); RED.view.dirty(false); RED.view.redraw(); diff --git a/red/library.js b/red/library.js index 2519d9417..0310ffc88 100644 --- a/red/library.js +++ b/red/library.js @@ -29,8 +29,7 @@ function init() { }); req.on('end', function() { storage.saveFlow(req.params[0],fullBody).then(function() { - res.writeHead(204, {'Content-Type': 'text/plain'}); - res.end(); + res.send(204); }).otherwise(function(err) { util.log("[red] Error loading flow '"+req.params[0]+"' : "+err); res.send(500); @@ -40,17 +39,14 @@ function init() { redApp.get("/library/flows",function(req,res) { storage.getAllFlows().then(function(flows) { - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.write(JSON.stringify(flows)); - res.end(); + res.json(flows); }); }); redApp.get(new RegExp("/library/flows\/(.*)"), function(req,res) { storage.getFlow(req.params[0]).then(function(data) { - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.write(data); - res.end(); + res.set('Content-Type', 'application/json'); + res.send(data); }).otherwise(function(err) { if (err) { util.log("[red] Error loading flow '"+req.params[0]+"' : "+err); @@ -67,13 +63,13 @@ function createLibrary(type) { redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) { var path = req.params[1]||""; storage.getLibraryEntry(type,path).then(function(result) { - res.writeHead(200, {'Content-Type': 'text/plain'}); if (typeof result === "string") { + res.writeHead(200, {'Content-Type': 'text/plain'}); res.write(result); + res.end(); } else { - res.write(JSON.stringify(result)); + res.json(result); } - res.end(); }).otherwise(function(err) { if (err) { util.log("[red] Error loading library entry '"+path+"' : "+err); diff --git a/red/server.js b/red/server.js index 3f2d246be..2b6b62b22 100644 --- a/red/server.js +++ b/red/server.js @@ -37,39 +37,29 @@ function createServer(_server,_settings) { flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json'; app.get("/nodes",function(req,res) { - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.write(redNodes.getNodeConfigs()); - res.end(); + res.json(redNodes.getNodeConfigs()); }); app.get("/flows",function(req,res) { - res.writeHead(200, {'Content-Type': 'text/plain'}); - res.write(JSON.stringify(redNodes.getConfig())); - res.end(); + res.json(redNodes.getConfig()); }); - app.post("/flows",function(req,res) { - var fullBody = ''; - req.on('data', function(chunk) { - fullBody += chunk.toString(); + app.post("/flows", + express.json(), + function(req,res) { + var flows = req.body; + storage.saveFlows(flows).then(function() { + res.json(204); + redNodes.setConfig(flows); + }).otherwise(function(err) { + util.log("[red] Error saving flows : "+err); + res.send(500,err.message); }); - req.on('end', function() { - try { - var flows = JSON.parse(fullBody); - storage.saveFlows(flows).then(function() { - res.writeHead(204, {'Content-Type': 'text/plain'}); - res.end(); - redNodes.setConfig(flows); - }).otherwise(function(err) { - util.log("[red] Error saving flows : "+err); - res.send(500, err.message); - }); - } catch(err) { - util.log("[red] Error saving flows : "+err); - res.send(400, "Invalid flow"); - } - }); - }); + }, + function(error,req,res,next) { + res.send(400,"Invalid Flow"); + } + ); } function start() {