forked from nodecg/nodecg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (50 loc) · 1.86 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var express = require('express'),
bodyParser = require('body-parser'),
app = express(),
fs = require('fs'),
server = require('http').createServer(app),
io = module.exports = require('socket.io').listen(server), //export our socket.io instance so modules may use it by requiring this file
config = require('./lib/config');
/**
* Express app setup
*/
app.use(express.static(__dirname + '/public'));
app.use('/components', express.static(__dirname + '/bower_components'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.engine('jade', require('jade').__express);
app.engine('html', require('ejs').renderFile);
app.engine('ejs', require('ejs').renderFile);
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
if (config.login.enabled) {
var login = require('./lib/login');
app.use(login);
}
var dashboard = require('./lib/dashboard');
app.use(dashboard);
var bundleViews = require('./lib/bundle_views');
app.use(bundleViews);
//load and mount express apps from bundles, if they have any
var bundlesDir = fs.readdirSync('bundles/');
bundlesDir.forEach(function (bndlName) {
// Skip if index.js doesn't exist
var bndlPath = 'bundles/' + bndlName + '/';
if (!fs.existsSync(bndlPath + "index.js")) {
return;
}
app.use(require('./' + bndlPath + "index.js"));
console.log('[lib/bundle/index.js] ' + bndlName + ' has an index.js, mounted');
});
io.set('log level', 1); // reduce logging
io.sockets.on('connection', function (socket) {
socket.on('message', function (data) {
io.sockets.json.send(data);
});
});
server.listen(config.port);
console.log("Listening on port " + config.port);