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

Tidy up REST interface

- Ensure application/json where appropriate
 - Use jquery api rather than d3
This commit is contained in:
Nick O'Leary 2014-04-15 22:30:07 +01:00
parent d702caa5be
commit d00624f9e3
3 changed files with 53 additions and 62 deletions

View File

@ -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("<strong>Error</strong>: "+resp,"error");
} else {
RED.notify("<strong>Error</strong>: 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("<strong>Error</strong>: "+xhr.responseText,"error");
} else {
RED.notify("<strong>Error</strong>: 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();

View File

@ -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);

View File

@ -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() {