forked from louischatriot/mongo-edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
131 lines (101 loc) · 3.79 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http')
, config = require('./lib/config')
, db = require('./lib/db')
, app // Will store our express app
, routes = require('./routes/routes')
, middlewares = require('./lib/middlewares')
, h4e = require('h4e');
app = express();
if (config.trustProxy) {
app.enable('trust proxy');
}
// Set up templating
h4e.setup({ app: app
, baseDir: config.templatesDir
, toCompileDirs: ['.']
, extension: 'mustache'
});
/**
* Middlewares
*
*/
app.use(express.bodyParser());
app.use(middlewares.ensureConnectionToMongo);
app.use(middlewares.commonRenderValues);
app.use(app.router); // Map routes
// Serving static files from paths that can't be confused with the webpages
app.use(config.baseUrl, express.static(__dirname));
app.get('favicon.ico', function (req, res, next) { return res.send(404); }); // No favicon
// Serve the webpages
if (config.baseUrl.length !== 0) { app.get(config.baseUrl, routes.index); } // Duplicate content but this page is not to be indexed right? And Express behaves badly with this kind of urls
app.get(config.baseUrl + '/', routes.index);
app.get(config.baseUrl + '/:collection', routes.collection);
app.get(config.baseUrl + '/:collection/new', routes.collectionCreate);
app.get(config.baseUrl + '/:collection/delete', routes.collectionDelete);
app.get(config.baseUrl + '/:collection/newDocument', routes.docCreate);
app.get(config.baseUrl + '/:collection/:id/edit', routes.docEdit);
app.get(config.baseUrl + '/:collection/:id/delete', routes.docDelete);
app.post(config.baseUrl + '/:collection/:id', routes.docChange);
/*
* Connect to database, then start server
*/
app.launchServer = function (cb) {
var callback = cb ? cb : function () {}
, self = this
;
db.connectToChosenDatabase(function (err) {
if (err) { return callback(err); }
self.apiServer = http.createServer(self); // Let's not call it 'server' we never know if Express will want to use this variable!
// Handle any connection error gracefully
self.apiServer.on('error', function () {
return callback("An error occured while launching the server, probably a server is already running on the same port!");
});
// Begin to listen. If the callback gets called, it means the server was successfully launched
self.apiServer.listen.apply(self.apiServer, [config.svPort, callback]);
});
};
/*
* Stop the server and then close the connection to the database
* No new connections will be accepted but existing ones will be served before closing
*/
app.stopServer = function (cb) {
var callback = cb ? cb : function () {}
, self = this;
self.apiServer.close(function () {
db.close();
console.log('Server was stopped and connection to the database closed');
callback();
});
};
/*
* If we executed this module directly, launch the server.
* If not, let the module which required server.js launch it.
*/
if (module.parent === null) { // Code to execute only when running as main
app.launchServer(function (err) {
if (err) {
console.log("An error occured, logging error and stopping the server");
console.log(err);
process.exit(1);
} else {
console.log('Server started on port ' + config.svPort);
}
});
}
/*
* If SIGINT is received (from Ctrl+C or from Upstart), gracefully stop the server then exit the process
* FOR NOW: commented out because browsers use hanging connections so every shutdown actually takes a few seconds (~5) if a browser connected to the server
* which makes for a way too long restart
*/
//process.on('SIGINT', function () {
//app.stopServer(function () {
//console.log('Exiting process');
//process.exit(0);
//});
//});
// exports
module.exports = app;