Register the express app to allow registration of urls.

This commit is contained in:
Michael Teeuw 2016-04-05 14:22:34 +02:00
parent f1dad4c9fb
commit 5420314784
3 changed files with 31 additions and 3 deletions

View File

@ -7,6 +7,7 @@ const Server = require(__dirname + '/server.js');
const spawn = require('child_process').spawn;
const electron = require('electron');
const defaultModules = require(__dirname + '/../modules/default/defaultmodules.js');
const path = require('path');
// Config
var config = {};
@ -80,6 +81,7 @@ function loadModule(module) {
var Module = require(helperPath);
var m = new Module();
m.setName(moduleName);
m.setPath(path.resolve(moduleFolder));
nodeHelpers.push(m);
}
}
@ -108,11 +110,12 @@ loadConfig(function(c) {
loadModules(modules);
var server = new Server(config, function(io) {
var server = new Server(config, function(app, io) {
console.log('Server started ...');
for (var h in nodeHelpers) {
var nodeHelper = nodeHelpers[h];
nodeHelper.setExpressApp(app);
nodeHelper.setSocketIO(io);
nodeHelper.start();
}

View File

@ -27,7 +27,7 @@ var Server = function(config, callback) {
});
if (typeof callback === 'function') {
callback(io);
callback(app, io);
}
};

View File

@ -6,6 +6,8 @@
*/
var Class = require('../../../js/class.js');
var express = require('express');
var path = require('path');
NodeHelper = Class.extend({
init: function() {
@ -26,7 +28,7 @@ NodeHelper = Class.extend({
console.log(this.name + ' received a socket notification: ' + notification + ' - Payload: ' + payload);
},
/* setName(data)
/* setName(name)
* Set the module name.
*
* argument name string - Module name.
@ -35,6 +37,15 @@ NodeHelper = Class.extend({
this.name = name;
},
/* setPath(path)
* Set the module path.
*
* argument name string - Module name.
*/
setPath: function(path) {
this.path = path;
},
/* sendSocketNotification(notification, payload)
* Send a socket notification to the node helper.
*
@ -45,6 +56,20 @@ NodeHelper = Class.extend({
this.io.of(this.name).emit(notification, payload);
},
/* setExpressApp(app)
* Sets the express app object for this module.
* This allows you to host files from the created webserver.
*
* argument app Express app - The Express app object.
*/
setExpressApp: function(app) {
this.expressApp = app;
var publicPath = this.path + '/public';
app.use('/' + this.name, express.static(publicPath));
},
/* setSocketIO(io)
* Sets the socket io object for this module.
* Binds message receiver.