Created
October 29, 2015 20:41
-
-
Save riteshsangwan/32b22b3be4cba6073abc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var http = require('http'); | |
var cache = {}; | |
var server; | |
var id = 'asdasddadasdasq44'; | |
app.use("/",express.static("public")); | |
var envPort = process.env.PORT || process.env.CF_INSTANCE_PORT || process.env.VCAP_APP_PORT; | |
var port = envPort || 3000; | |
var settings = { | |
httpAdminRoot:"/red", | |
httpNodeRoot: "/api", | |
userDir:"/tmp/tmp-node", | |
uiPort: port, | |
functionGlobalContext: { } // enables global context | |
}; | |
app.get('/start', function(req, res, next) { | |
// mount the sub app | |
var RED = require("node-red"); | |
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); | |
RED.start(); | |
cache[id] = RED; | |
}); | |
app.get('/stop', function(req, res, next) { | |
var RED = cache[id]; | |
RED.stop(); | |
var routes = app._router.stack; | |
routes.forEach(function(route, index) { | |
if(route.path === '/red') { | |
// remove the mounted app | |
routes.splice(index, 1); | |
} | |
}); | |
}); | |
server = http.createServer(app); | |
server.listen(port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "iot-test", | |
"description": "IOT Test", | |
"version": "1.0.0", | |
"contributors": [], | |
"repository": { | |
"type": "git", | |
"url": "" | |
}, | |
"dependencies": { | |
"express": "^4.13.3", | |
"node-red": "^0.11.2" | |
}, | |
"devDependencies": { | |
}, | |
"engines": { | |
"node": "0.12.7" | |
}, | |
"scripts": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment