2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 var http = require('http');
17 var https = require('https');
18 var util = require("util");
19 var express = require("express");
20 var crypto = require("crypto");
21 var nopt = require("nopt");
22 var path = require("path");
23 var RED = require("./red/red.js");
24 var fs = require('fs')
28 var settingsFile = "./settings";
40 nopt.invalidHandler = function(k,v,t) {
41 // TODO: console.log(k,v,t);
44 var parsedArgs = nopt(knownOpts,shortHands,process.argv,2)
46 if (parsedArgs.help) {
47 console.log("Node-RED v"+RED.version());
48 console.log("Usage: node red.js [-v] [-?] [--settings settings.js] [flows.json]");
50 console.log("Options:");
51 console.log(" -s, --settings FILE use specified settings file");
52 console.log(" -v enable verbose output");
53 console.log(" -?, --help show usage");
55 console.log("Documentation can be found at http://nodered.org");
58 if (parsedArgs.argv.remain.length > 0) {
59 flowFile = parsedArgs.argv.remain[0];
62 if (parsedArgs.settings) {
63 settingsFile = parsedArgs.settings;
66 var settings = require(settingsFile);
68 if (err.code == 'MODULE_NOT_FOUND') {
69 console.log("Unable to load settings file "+settingsFile);
77 settings.verbose = true;
80 if (settings.enableHttps) {
81 //server = https.createServer(settings.https,function(req,res){app(req,res);});
82 server = https.createServer({
83 key: fs.readFileSync('certs/node-key.pem'),
84 cert: fs.readFileSync('certs/node-cert.pem'),
85 requireHttps : true},function(req,res){app(req,res);});
87 server = http.createServer(function(req,res){app(req,res);});
89 server.setMaxListeners(0);
91 function formatRoot(root) {
95 if (root.slice(-1) != "/") {
101 if (settings.httpRoot === false) {
102 settings.httpAdminRoot = false;
103 settings.httpNodeRoot = false;
105 settings.httpRoot = settings.httpRoot||"/";
106 settings.disableEditor = settings.disableEditor||false;
109 if (settings.httpAdminRoot !== false) {
110 settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/");
111 settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth;
113 settings.disableEditor = true;
116 if (settings.httpNodeRoot !== false) {
117 settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/");
118 settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth;
121 settings.uiPort = settings.uiPort||1880;
122 settings.uiHost = settings.uiHost||"0.0.0.0";
123 var appDir = path.dirname(require.main.filename);
124 if (settings.flowFile != null && settings.flowFile.indexOf(appDir) != -1){
125 settings.flowFile = flowFile || settings.flowFile;
127 settings.flowFile = flowFile || appDir + "/" + settings.flowFile;
130 RED.init(server,settings);
132 if (settings.httpAdminRoot !== false && settings.httpAdminAuth) {
133 app.use(settings.httpAdminRoot,
134 express.basicAuth(function(user, pass) {
135 return user === settings.httpAdminAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpAdminAuth.pass;
139 if (settings.httpNodeRoot !== false && settings.httpNodeAuth) {
140 app.use(settings.httpNodeRoot,
141 express.basicAuth(function(user, pass) {
142 return user === settings.httpNodeAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpNodeAuth.pass;
146 if (settings.httpAdminRoot !== false) {
147 app.use(settings.httpAdminRoot,RED.httpAdmin);
149 if (settings.httpNodeRoot !== false) {
150 app.use(settings.httpNodeRoot,RED.httpNode);
153 if (settings.httpStatic) {
154 settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth;
155 if (settings.httpStaticAuth) {
157 express.basicAuth(function(user, pass) {
158 return user === settings.httpStaticAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpStaticAuth.pass;
162 app.use("/",express.static(settings.httpStatic));
165 function getListenPath() {
166 var listenPath = 'http'+(settings.enableHttps?'s':'')+'://'+
167 (settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+
169 if (settings.httpAdminRoot !== false) {
170 listenPath += settings.httpAdminRoot;
171 } else if (settings.httpStatic) {
177 RED.start().then(function() {
178 if (settings.httpAdminRoot !== false || settings.httpNodeRoot !== false || settings.httpStatic) {
179 server.on('error', function(err) {
180 if (err.errno === "EADDRINUSE") {
181 util.log('[red] Unable to listen on '+getListenPath());
182 util.log('[red] Error: port in use');
184 util.log('[red] Uncaught Exception:');
193 server.listen(settings.uiPort,settings.uiHost,function() {
194 if (settings.httpAdminRoot === false) {
195 util.log('[red] Admin UI disabled');
197 process.title = 'node-red';
198 util.log('[red] Server now running at '+getListenPath());
201 util.log('[red] Running in headless mode');
203 }).otherwise(function(err) {
204 util.log("[red] Failed to start server:");
213 process.on('uncaughtException',function(err) {
214 util.log('[red] Uncaught Exception:');
223 process.on('SIGINT', function () {
225 // TODO: need to allow nodes to close asynchronously before terminating the
226 // process - ie, promises