seneca.ready(function(err){ if (!err) { var config = seneca.export('options'); console.log("userDir = ", config.nodered.userDir); // Initialise the runtime with a server and settings console.log("Node red config = ", config.nodered); RED.init(server,config.nodered); // Serve the editor UI from /red if (config.nodered.httpAdminRoot) { app.use(config.nodered.httpAdminRoot,RED.httpAdmin); } // Serve the http nodes UI from /api app.use(config.nodered.httpNodeRoot,RED.httpNode); server.listen(config.main.port); console.log("Listening on port ", config.main.port); // Start the runtime RED.start(); } else { console.error(err); } });
function start(options, callback) { // Create an Express app var app = express(); // Add a simple route for static content served from 'public' app.use('/', express.static('public')); // Create a server var server = http.createServer(app); // jshint ignore:line // Create the settings object - see default settings.js file for other // options var settings; if (options && options.settings) { settings = options.settings; } else { settings = { httpAdminRoot : '/red', httpNodeRoot : '/redapi', userDir : 'nodered/', nodesDir : '../nodes', flowFile : 'node-red-flows.json', functionGlobalContext : {} // enables global context }; } // Initialise the runtime with a server and settings RED.init(server, settings); // Serve the editor UI from /red app.use(settings.httpAdminRoot, RED.httpAdmin); // Serve the http nodes UI from /api app.use(settings.httpNodeRoot, RED.httpNode); var port = options ? options.port || 3001 : 3001; server.listen(port); // Start the runtime RED.start(); setTimeout(callback, 5000); }
start: function () { // Create an Express app var app = express(); // Add a simple route for static content served from 'public' app.use("/", express.static("public")); // Create a server var server = http.createServer(app); // Create the settings object - see default settings.js file for other options var settings = { httpAdminRoot: "/red", userDir: "/Users/gercan/.nodered", nodesDir: "/Users/gercan/workspaces/devNation2015Demo/Gateway/node-red/nodes", functionGlobalContext: {}, verbose: true, logging: { console: { // Level of logging to be recorded. Options are: // fatal - only those errors which make the application unusable should be recorded // error - record errors which are deemed fatal for a particular request + fatal errors // warn - record problems which are non fatal + errors + fatal errors // info - record information about the general running of the application + warn + error + fatal errors // debug - record information which is more verbose than info + info + warn + error + fatal errors // trace - record very detailed logging + debug + info + warn + error + fatal errors level: "trace", // Whether or not to include metric events in the log output metrics: false } } }; // Initialise the runtime with a server and settings RED.init(server, settings); // Serve the editor UI from /red app.use(settings.httpAdminRoot, RED.httpAdmin); server.listen(8000); // Start the runtime RED.start(); }
flowFile: 'flows.json', flowFilePretty: true, verbose:false, httpAdminRoot:"/", httpNodeRoot: "/", userDir: path.join(__dirname , 'node-red'), functionGlobalContext: { riddlesEvents : riddlesEvents } , // enables global context ui:{ // https://github.com/andrei-tatar/node-red-contrib-ui/blob/master/ui.js#L162 title: 'riddler back office' } }; // Initialise the runtime with a server and settings RED.init(server,settings); // Serve the editor UI from /red app.use(settings.httpAdminRoot,RED.httpAdmin); // Serve the http nodes UI from /api app.use(settings.httpNodeRoot,RED.httpNode); server.listen(process.env.node_red_port || 80); // Start the runtime RED.start() .then(function(){ var d = Discover({ multicast : process.env.multicastAddr || '239.0.0.0'
// Running on Bluemix. Parse the port and host that we've been assigned. var env = JSON.parse(process.env.VCAP_SERVICES); console.log('VCAP_SERVICES: %s', process.env.VCAP_SERVICES); // Also parse Cloudant settings. var couchService = env['cloudantNoSQLDB'][0]['credentials']; } if (!couchService) { console.log("Failed to find Cloudant service"); if (process.env.NODE_RED_STORAGE_NAME) { console.log(" - using NODE_RED_STORAGE_NAME environment variable: "+process.env.NODE_RED_STORAGE_NAME); } throw new Error("No cloudant service found"); } settings.couchUrl = couchService.url; // Initialise the runtime with a server and settings RED.init( httpServer, settings ); // Serve the editor UI from /red app.use( settings.httpAdminRoot, RED.httpAdmin ); // Serve the http nodes UI from /api app.use( settings.httpNodeRoot, RED.httpNode ); httpServer.listen( port, function(){ console.log('App listening on port: ', port); }); // Start the runtime RED.start();
function start(options, callback) { var app = null; var server = null; var separateSever // Atul : if server is passed as part of options, same will be used. // server could have been passed as explicit parameter instead of part of options. but this is being done // to support backward compatibility. // in future, signature of this can be chagned. if (!options.settings.server) { app = express(); // Create a server server = http.createServer(app); // jshint ignore:line } else { app = options.settings.server; server = options.settings.server.server; } // Add a simple route for static content served from 'public' app.use('/', express.static('public')); // Create the settings object - see default settings.js file for other // options var settings; if (options && options.settings) { settings = options.settings; } else { settings = { httpAdminRoot : '/red', httpNodeRoot : '/redapi', userDir : 'nodered/', nodesDir : '../nodes', flowFile : 'node-red-flows.json', functionGlobalContext : {} // enables global context }; } // Initialise the runtime with a server and settings RED.init(server, settings); // Serve the editor UI from /red app.use(settings.httpAdminRoot, RED.httpAdmin); // Serve the http nodes UI from /api app.use(settings.httpNodeRoot, RED.httpNode); var adminApp = RED.httpAdmin; var redNodes = RED.nodes; if (!options.settings.server) { var port = options ? options.port || 3001 : 3001; server.listen(port); RED.start().then(function () { return callback(); }); } else { // Start the runtime - removing earlier timeout implementation!! RED.start().then(function () { return callback(); }).otherwise(function (err) { console.log('**ERROR : NODE RED WAS NOT STARTED ***' , err); }); } }
// main HTTP server function function run() { console.log("Running HTTP server at port " + port); var app = express(); server = http.Server(app); io = io.listen(server); app.use(favicon("client/img/favicon.ico")); app.use(morgan('dev')); app.use(cookieParser()); app.use(session({ secret: 'jcvsnasdovhjdsfanbdwkjv', saveUninitialized: true, resave: true, store: new MongoStore({ url: db_url , autoReconnect: true, safe: true}) })); app.use(log_url); app.use(static_file_handler2); app.use(preprocess_api_calls); app.use(json_parser); app.use(body_parser); require('./config/passport.js'); // routes app.get('/', function (req, res) { res.setHeader("Content-Type", "text/html"); res.end(root_content); }); app.get('/login', function (req, res) { res.setHeader("Content-Type", "text/html"); res.end(login_content); }); app.post('/login', function (req, res, next) { bl.verify_user(req.body.username, req.body.password, function (err, user) { if (err) { bl.get_user({ data: { username: req.body.username} }, function (err, data) { var msg = ""; if (err) { msg = "Login failed"; } else { msg = "User cannot login - wrong password or user inactive."; } res.redirect('/login?msg=' + encodeURI(msg)); }); } else { req.session.is_authenticated = true req.session.user = user; res.redirect('/admin.html'); } }); }); app.get('/logout', function (req, res) { req.session.destroy(); res.redirect('/login'); }); app.get('/help', function (req, res) { res.setHeader("Content-Type", "text/html"); res.end(help_content); }); app.post('/handler', ensure_authenticated, main_handler); app.route('/api') .get(function(req, res) { var response = { "collections": [ { "name": "nodes", "href": "/nodes" }, { "name": "clusters", "href": "/clusters" }, { "name": "sensors", "href": "/sensors" }, { "name": "measurements", "href": "/measurements" } ] } res.json(response); }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET').json( req.method + ' method is not supported.' ); }); app.route('/api/measurements') .get(function(req, res) { bl.get_all_measurements(req, function(callback) { if (callback.error) res.status(callback.status).json(callback.error); else res.json(callback); }); }) .post(function(req, res) { if (!req.body.ts) { req.body.ts = new Date(); } io.emit('measurements', req.body); bl.api_add_sensor_measurement(req.body, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.status(callback.status).json(callback.message); }); }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET,POST').json( req.method + ' method is not supported.' ); }); app.route('/api/measurements/:measurement_id') .get(function(req, res) { bl.get_sensor_measurement(req.params.measurement_id, function(callback) { if (callback.error) res.status(callback.status).json(callback.error); else res.json(callback); }) }) .put(function(req, res) { bl.update_sensor_measurement(req, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback.message); }) }) .delete(function(req, res) { bl.delete_sensor_measurement(req.params.measurement_id, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback.message); }) }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET,PUT,DELETE').json( req.method + ' method is not supported.' ); }); app.route('/api/nodes') .get(function(req, res) { bl.api_get_nodes(req, function(callback) { if (callback.error) res.status(callback.status).json(callback.error); else res.json(callback); }); }) .post(function(req, res) { bl.api_add_node(req.body, function(callback) { if(callback.error) res.json(callback.error).status(callback.status); else res.json(callback.message).status(callback.status); }) }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET,POST').json( req.method + ' method is not supported.' ); }); app.route('/api/nodes/:node_id') .get(function(req, res) { bl.api_get_node(req.params.node_id, function (callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback); }) }) .put(function(req, res) { bl.api_update_node(req, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback.message); }) }) .delete(function(req, res) { bl.api_delete_node(req.params.node_id, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback.message); }) }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET,PUT,DELETE').json( req.method + ' method is not supported.' ); }); app.route('/api/clusters') .get(function(req, res) { bl.api_get_clusters(req, function (err, clusters) { if (err) return res.json(err); res.json(clusters); }); }) .post(function(req, res) { bl.api_add_cluster(req.body, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.status(callback.status).json(callback.message); }); }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET,POST').json( req.method + ' method is not supported.' ); }); app.route('/api/clusters/:cluster_id') .get(function(req, res) { bl.api_get_cluster(req.params.cluster_id, function (callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback); }) }) .put(function(req, res) { bl.api_update_cluster(req, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback.message); }) }) .delete(function(req, res) { bl.api_delete_cluster(req.params.cluster_id, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback.message); }) }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET,PUT,DELETE').json( req.method + ' method is not supported.' ); }); app.route('/api/sensors') .get(function(req, res) { bl.get_sensors(req, function (callback) { if (callback.error) res.status(callback.status).json(callback.error); else res.json(callback); }); }) .post(function(req, res) { bl.add_sensor(req.body, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.status(callback.status).json(callback.message) }); }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET,POST').json( req.method + ' method is not supported.' ); }); app.route('/api/sensors/:sensor_id') .get(function(req, res) { bl.get_sensor(req.params.sensor_id, function(callback) { if (callback.error) res.status(callback.status).json(callback.error); else res.json(callback); }) }) .put(function(req, res) { bl.update_sensor(req, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback.message); }) }) .delete(function(req, res) { bl.delete_sensor(req.params.id, function(callback) { if(callback.error) res.status(callback.status).json(callback.error); else res.json(callback.message); }) }) .all(function(req, res) { res.status(405).header('Access-Control-Allow-Methods', 'GET,PUT,DELETE').json( req.method + ' method is not supported.' ); }); app.get('/api/*', main_handler); app.post('/api/*', main_handler); app.use(passport.initialize()); app.use(passport.session()); app.use(function(req, res, next){ if(typeof req.query.entityID != 'undefined') { idp = url.parse(req.query.entityID).hostname; passport._strategies.config2._saml.options.entryPoint = 'https://' + idp + '/simplesaml/saml2/idp/SSOService.php'; } next(); }); app.get('/aai', passport.authenticate('config1', { failureRedirect: '/login', failureFlash: true }), function(req, res) { res.redirect('/login'); }); app.get('/loginaai', passport.authenticate('config2', { failureRedirect: '/login', failureFlash: true }), function(req, res) { res.redirect('/login'); }); app.post('/assert', passport.authenticate('config2', { failureRedirect: '/', failureFlash: true }), function(req, res) { bl.get_user({ data: { username: req.session.passport.user.userName } }, function (err, data) { if (err) { var rnd = Math.random().toString(36).substring(7); new_user = { data: { username: req.session.passport.user.userName, full_name: req.session.passport.user.firstName + " " + req.session.passport.user.lastName, type: "normal", pwd1: rnd, pwd2: rnd }}; bl.new_user(new_user, function () { req.session.is_authenticated = true; req.session.user = req.session.passport.user.userName; res.redirect('/admin.html'); }); } else { req.session.is_authenticated = true; req.session.user = req.session.passport.user.userName; res.redirect('/admin.html'); } }); }); app.use(function(err, req, res, next) { res.status(404).json("The requested resource could not be found!"); }); app.get('/umko/*', ensure_authenticated); var redSettings = { httpAdminRoot: "/umko", paletteCategories: ['subflows', 'estoritve', 'input', 'output', 'function', 'social', 'storage', 'analysis', 'advanced'], httpNodeRoot: "/umkoapi", functionGlobalContext: { } // enables global context }; nodeRed.init(server,redSettings); app.use(redSettings.httpAdminRoot,nodeRed.httpAdmin); app.use(redSettings.httpNodeRoot,nodeRed.httpNode); // ok, start the server server.listen(port); nodeRed.start(); }
rimraf(userDir, function () { // Create the settings object - see default settings.js file for other options var settings = { httpAdminRoot: httpAdminRoot, httpNodeRoot: httpNodeRoot, userDir: userDir, nodesDir: nodesDir, functionGlobalContext: { } // enables global context }; // Initialise the runtime with a server and settings RED.init(this.server, settings); // Serve the editor UI from settings.httpAdminRoot app.use(settings.httpAdminRoot, RED.httpAdmin); // Serve the http nodes UI from settings.httpNodeRoot app.use(settings.httpNodeRoot, RED.httpNode); var port = this.getDictionary().getNumber('port', this.dic_port.defaultValue); if (!port) { done(new Error('"'+this.getName()+'" attribute "port" must be set')); return; } this.server.on('connection', function (client) { var id = shortid.generate(); this.clients[id] = client; client.on('close', function () { delete this.clients[id]; }.bind(this)); }.bind(this)); this.server.listen(port, function () { this.log.info(this.toString(), '"'+this.getName()+'" server started at http://0.0.0.0:'+port+httpAdminRoot); // Start the runtime RED.start() .then(function () { // TODO get rid of this as soon as node-red fixes the issue // https://github.com/node-red/node-red/pull/685 return when.promise(function (resolve) { setTimeout(function () { resolve(); }, 2000); }); }) .then(function () { this.preUpdateFlows(this.getDictionary().getString('flows')) .then(function () { done(); }) .catch(function () { this.log.warn(this.toString(), '"'+this.getName()+'" unable to set flows ('+err.message+')'); }.bind(this)); }.bind(this)) .catch(function (err) { this.log.warn(this.toString(), '"'+this.getName()+'" unable to start NodeRED ('+err.message+')'); done(err); }.bind(this)); }.bind(this)); // do not rebind handlers when component is restarting this.getDictionary().emitter .removeAllListeners('port') .removeAllListeners('httpAdminRoot') .removeAllListeners('httpNodeRoot') .removeAllListeners('userDir') .removeAllListeners('flows'); this.getDictionary().on('flows', function (flows) { this.preUpdateFlows(flows) .catch(function (err) { this.log.warn(this.toString(), '"'+this.getName()+'" unable to set flows ('+err.message+')'); }.bind(this)); }.bind(this)); this.getDictionary().on('port', function () { this.needRestart = true; }.bind(this)); this.getDictionary().on('httpAdminRoot', function () { this.needRestart = true; }.bind(this)); this.getDictionary().on('httpNodeRoot', function () { this.needRestart = true; }.bind(this)); this.getDictionary().on('userDir', function () { this.needRestart = true; }.bind(this)); }.bind(this));
httpNodeRoot: "/control-api", userDir:path.join(__dirname, '../control/node-red'), flowFile: "autom-8-control-flows.json", functionGlobalContext: { } // enables global context }; // debug('Initialising RED'); //Parent app, assumes this will be 'required' from a higher level module //that exports an express app. APP = module.parent.exports; //Server attached to parent app, assumes that APP has a setting 'server' already setup. server = APP.get('server'); // Initialise the runtime with a server and settings RED.init(server, REDsettings); //Add the two node-red apps to the parent app routes APP.use(REDsettings.httpAdminRoot, RED.httpAdmin); APP.use(REDsettings.httpNodeRoot, RED.httpNode); server.on('listening', function() { debug('Starting RED.'); RED.start(); }); //This module exports nothing at this stage. module.exports = {};