-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.js
75 lines (49 loc) · 1.89 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Express application init
var express = require("express"),
// Path parsing module
path = require("path"),
// Configuration
config = require("./libs/config"),
// Route map
routeMap = require("./app/controllers/route-map"),
// Module to parse JSON in the requests
bodyParser = require("body-parser"),
// Error handler
errorHandler = require("errorhandler"),
// Includes the support for PUT and DELETE methods
methodOverride = require("method-override"),
// Favicon serving
favicon = require("serve-favicon"),
app = express(),
socketIO = require("socket.io"),
socketHandler = require("./app/controllers/socketHandler"),
predefinedDataLoader = require("./app/controllers/predefinedDataLoader"),
connectionString = config.get('mongodb:uri'),
MongoClient = require('mongodb').MongoClient;
// Application configuration
app.use(favicon(path.join(__dirname, "public/images/favicon.ico")));
app.use(bodyParser());
app.use(methodOverride());
app.set("views", __dirname + "/app/views");
app.set("view engine", "jade");
app.use(errorHandler());
// Static file server watching on the "public" folder and returning the static content
app.use(express.static(path.join(__dirname, "public")));
// Initialize connection once
MongoClient.connect(connectionString, function(connectionError, database) {
if (connectionError) {
console.error(connectionError.message);
} else {
global.activitystreamsDB = database;
// Set up server and sockets
var server = require("http").createServer(app),
io = socketIO.listen(server, { log: false });
server.listen(config.get("port"));
// Set up socket connections handler
socketHandler(io);
// Generate route map
routeMap(app);
// Load predefined data if hasn't already
predefinedDataLoader();
}
});