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

104 lines
3.5 KiB
JavaScript
Raw Normal View History

2013-09-05 16:02:48 +02:00
/**
* Copyright 2013 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var fs = require('fs');
var util = require('util');
var createUI = require("./ui");
var redNodes = require("./nodes");
var host = require('os').hostname();
var app = null;
var server = null;
function createServer(_server,settings) {
server = _server;
app = createUI(settings);
//TODO: relocated user dir
var rulesfile = process.argv[2] || 'flows_'+host+'.json';
2013-09-20 15:21:45 +02:00
fs.exists("lib/",function(exists) {
if (!exists) {
fs.mkdir("lib");
}
});
2013-09-05 16:02:48 +02:00
app.get("/nodes",function(req,res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(redNodes.getNodeConfigs());
res.end();
});
app.get("/flows",function(req,res) {
fs.exists(rulesfile, function (exists) {
if (exists) {
res.sendfile(rulesfile);
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("[]");
res.end();
}
});
});
app.post("/flows",function(req,res) {
var fullBody = '';
req.on('data', function(chunk) {
fullBody += chunk.toString();
});
req.on('end', function() {
res.writeHead(204, {'Content-Type': 'text/plain'});
res.end();
fs.writeFile(rulesfile, fullBody, function(err) {
if(err) {
util.log(err);
} else {
redNodes.setConfig(JSON.parse(fullBody));
2013-09-05 16:02:48 +02:00
}
});
});
});
console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Loading palette nodes");
util.log("------------------------------------------");
redNodes.load();
util.log("");
util.log('You may ignore any errors above here if they are for');
util.log('nodes you are not using. The nodes indicated will not');
util.log('be available in the main palette until any missing');
util.log('modules are installed, typically by running:');
util.log(' npm install {the module name}');
util.log('or any other errors are resolved');
util.log("------------------------------------------");
fs.exists(rulesfile, function (exists) {
if (exists) {
util.log("[red] Loading workspace flow : "+rulesfile);
2013-09-05 16:02:48 +02:00
fs.readFile(rulesfile,'utf8',function(err,data) {
redNodes.setConfig(JSON.parse(data));
2013-09-05 16:02:48 +02:00
});
}
});
return app;
}
module.exports = {
init: createServer
}
module.exports.__defineGetter__("app", function() { return app });
module.exports.__defineGetter__("server", function() { return server });