mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Tidy up REST interface
- Ensure application/json where appropriate - Use jquery api rather than d3
This commit is contained in:
		| @@ -80,30 +80,35 @@ var RED = function() { | |||||||
|             $("#btn-icn-deploy").addClass('spinner'); |             $("#btn-icn-deploy").addClass('spinner'); | ||||||
|             RED.view.dirty(false); |             RED.view.dirty(false); | ||||||
|              |              | ||||||
|             d3.xhr("flows").header("Content-type", "application/json") |             $.ajax({ | ||||||
|                            .post(JSON.stringify(nns),function(err,resp) { |                 url:"flows", | ||||||
|                     $("#btn-icn-deploy").removeClass('spinner'); |                 type: "POST", | ||||||
|                     $("#btn-icn-deploy").addClass('icon-upload'); |                 data: JSON.stringify(nns), | ||||||
|                     if (resp && resp.status == 204) { |                 contentType: "application/json; charset=utf-8" | ||||||
|                         RED.notify("Successfully deployed","success"); |             }).done(function(data,textStatus,xhr) { | ||||||
|                         RED.nodes.eachNode(function(node) { |                 RED.notify("Successfully deployed","success"); | ||||||
|                             if (node.changed) { |                 RED.nodes.eachNode(function(node) { | ||||||
|                                 node.dirty = true; |                     if (node.changed) { | ||||||
|                                 node.changed = false; |                         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); |  | ||||||
|                     } |                     } | ||||||
|  |                 }); | ||||||
|  |                 // 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() { |     function loadFlows() { | ||||||
|         d3.json("flows",function(nodes) { |         $.getJSON("flows",function(nodes) { | ||||||
|                 RED.nodes.import(nodes); |                 RED.nodes.import(nodes); | ||||||
|                 RED.view.dirty(false); |                 RED.view.dirty(false); | ||||||
|                 RED.view.redraw(); |                 RED.view.redraw(); | ||||||
|   | |||||||
| @@ -29,8 +29,7 @@ function init() { | |||||||
|             }); |             }); | ||||||
|             req.on('end', function() { |             req.on('end', function() { | ||||||
|                 storage.saveFlow(req.params[0],fullBody).then(function() { |                 storage.saveFlow(req.params[0],fullBody).then(function() { | ||||||
|                     res.writeHead(204, {'Content-Type': 'text/plain'}); |                     res.send(204); | ||||||
|                     res.end(); |  | ||||||
|                 }).otherwise(function(err) { |                 }).otherwise(function(err) { | ||||||
|                     util.log("[red] Error loading flow '"+req.params[0]+"' : "+err); |                     util.log("[red] Error loading flow '"+req.params[0]+"' : "+err); | ||||||
|                     res.send(500); |                     res.send(500); | ||||||
| @@ -40,17 +39,14 @@ function init() { | |||||||
|      |      | ||||||
|     redApp.get("/library/flows",function(req,res) { |     redApp.get("/library/flows",function(req,res) { | ||||||
|             storage.getAllFlows().then(function(flows) { |             storage.getAllFlows().then(function(flows) { | ||||||
|                 res.writeHead(200, {'Content-Type': 'text/plain'}); |                 res.json(flows); | ||||||
|                 res.write(JSON.stringify(flows)); |  | ||||||
|                 res.end(); |  | ||||||
|             }); |             }); | ||||||
|     }); |     }); | ||||||
|      |      | ||||||
|     redApp.get(new RegExp("/library/flows\/(.*)"), function(req,res) { |     redApp.get(new RegExp("/library/flows\/(.*)"), function(req,res) { | ||||||
|             storage.getFlow(req.params[0]).then(function(data) { |             storage.getFlow(req.params[0]).then(function(data) { | ||||||
|                 res.writeHead(200, {'Content-Type': 'text/plain'}); |                 res.set('Content-Type', 'application/json'); | ||||||
|                 res.write(data); |                 res.send(data); | ||||||
|                 res.end(); |  | ||||||
|             }).otherwise(function(err) { |             }).otherwise(function(err) { | ||||||
|                 if (err) { |                 if (err) { | ||||||
|                     util.log("[red] Error loading flow '"+req.params[0]+"' : "+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) { |     redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) { | ||||||
|             var path = req.params[1]||""; |             var path = req.params[1]||""; | ||||||
|             storage.getLibraryEntry(type,path).then(function(result) { |             storage.getLibraryEntry(type,path).then(function(result) { | ||||||
|                 res.writeHead(200, {'Content-Type': 'text/plain'}); |  | ||||||
|                 if (typeof result === "string") { |                 if (typeof result === "string") { | ||||||
|  |                     res.writeHead(200, {'Content-Type': 'text/plain'}); | ||||||
|                     res.write(result); |                     res.write(result); | ||||||
|  |                     res.end();  | ||||||
|                 } else { |                 } else { | ||||||
|                     res.write(JSON.stringify(result)); |                     res.json(result); | ||||||
|                 } |                 } | ||||||
|                 res.end();  |  | ||||||
|             }).otherwise(function(err) { |             }).otherwise(function(err) { | ||||||
|                 if (err) { |                 if (err) { | ||||||
|                     util.log("[red] Error loading library entry '"+path+"' : "+err); |                     util.log("[red] Error loading library entry '"+path+"' : "+err); | ||||||
|   | |||||||
| @@ -37,39 +37,29 @@ function createServer(_server,_settings) { | |||||||
|     flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json'; |     flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json'; | ||||||
|      |      | ||||||
|     app.get("/nodes",function(req,res) { |     app.get("/nodes",function(req,res) { | ||||||
|             res.writeHead(200, {'Content-Type': 'text/plain'}); |         res.json(redNodes.getNodeConfigs()); | ||||||
|             res.write(redNodes.getNodeConfigs()); |  | ||||||
|             res.end(); |  | ||||||
|     }); |     }); | ||||||
|      |      | ||||||
|     app.get("/flows",function(req,res) { |     app.get("/flows",function(req,res) { | ||||||
|             res.writeHead(200, {'Content-Type': 'text/plain'}); |         res.json(redNodes.getConfig()); | ||||||
|             res.write(JSON.stringify(redNodes.getConfig())); |  | ||||||
|             res.end(); |  | ||||||
|     }); |     }); | ||||||
|      |      | ||||||
|     app.post("/flows",function(req,res) { |     app.post("/flows", | ||||||
|             var fullBody = ''; |         express.json(), | ||||||
|             req.on('data', function(chunk) { |         function(req,res) { | ||||||
|                     fullBody += chunk.toString(); |             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 {  |         function(error,req,res,next) { | ||||||
|                         var flows = JSON.parse(fullBody); |             res.send(400,"Invalid Flow"); | ||||||
|                         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 start() { | function start() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user