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");
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;
81 server = https.createServer(settings.https,function(req,res){app(req,res);});
83 server = http.createServer(function(req,res){app(req,res);});
85 server.setMaxListeners(0);
87 function formatRoot(root) {
91 if (root.slice(-1) != "/") {
97 if (settings.httpRoot === false) {
98 settings.httpAdminRoot = false;
99 settings.httpNodeRoot = false;
101 settings.httpRoot = settings.httpRoot||"/";
102 settings.disableEditor = settings.disableEditor||false;
105 if (settings.httpAdminRoot !== false) {
106 settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/");
107 settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth;
109 settings.disableEditor = true;
112 if (settings.httpNodeRoot !== false) {
113 settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/");
114 settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth;
117 settings.uiPort = settings.uiPort||1880;
118 settings.uiHost = settings.uiHost||"0.0.0.0";
119 var appDir = path.dirname(require.main.filename);
120 if (settings.flowFile != null && settings.flowFile.indexOf(appDir) != -1){
121 settings.flowFile = flowFile || settings.flowFile;
123 settings.flowFile = flowFile || appDir + "/" + settings.flowFile;
126 RED.init(server,settings);
128 if (settings.httpAdminRoot !== false && settings.httpAdminAuth) {
129 app.use(settings.httpAdminRoot,
130 express.basicAuth(function(user, pass) {
131 return user === settings.httpAdminAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpAdminAuth.pass;
135 if (settings.httpNodeRoot !== false && settings.httpNodeAuth) {
136 app.use(settings.httpNodeRoot,
137 express.basicAuth(function(user, pass) {
138 return user === settings.httpNodeAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpNodeAuth.pass;
142 if (settings.httpAdminRoot !== false) {
143 app.use(settings.httpAdminRoot,RED.httpAdmin);
145 if (settings.httpNodeRoot !== false) {
146 app.use(settings.httpNodeRoot,RED.httpNode);
149 if (settings.httpStatic) {
150 settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth;
151 if (settings.httpStaticAuth) {
153 express.basicAuth(function(user, pass) {
154 return user === settings.httpStaticAuth.user && crypto.createHash('md5').update(pass,'utf8').digest('hex') === settings.httpStaticAuth.pass;
158 app.use("/",express.static(settings.httpStatic));
161 function getListenPath() {
162 var listenPath = 'http'+(settings.https?'s':'')+'://'+
163 (settings.uiHost == '0.0.0.0'?'127.0.0.1':settings.uiHost)+
165 if (settings.httpAdminRoot !== false) {
166 listenPath += settings.httpAdminRoot;
167 } else if (settings.httpStatic) {
173 RED.start().then(function() {
174 if (settings.httpAdminRoot !== false || settings.httpNodeRoot !== false || settings.httpStatic) {
175 server.on('error', function(err) {
176 if (err.errno === "EADDRINUSE") {
177 util.log('[red] Unable to listen on '+getListenPath());
178 util.log('[red] Error: port in use');
180 util.log('[red] Uncaught Exception:');
189 server.listen(settings.uiPort,settings.uiHost,function() {
190 if (settings.httpAdminRoot === false) {
191 util.log('[red] Admin UI disabled');
193 process.title = 'node-red';
194 util.log('[red] Server now running at '+getListenPath());
197 util.log('[red] Running in headless mode');
199 }).otherwise(function(err) {
200 util.log("[red] Failed to start server:");
209 process.on('uncaughtException',function(err) {
210 util.log('[red] Uncaught Exception:');
219 process.on('SIGINT', function () {
221 // TODO: need to allow nodes to close asynchronously before terminating the
222 // process - ie, promises