forked from rendrjs/rendr-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (68 loc) · 1.68 KB
/
index.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
var express = require('express'),
rendr = require('rendr'),
env = require('./server/lib/env'),
mw = require('./server/middleware'),
DataAdapter = require('./server/lib/data_adapter'),
app,
server;
app = express();
/**
* Initialize Express middleware stack.
*/
function initMiddleware() {
app.use(express.compress());
app.use(express.static(__dirname + '/public'));
app.use(express.logger());
app.use(express.bodyParser());
/**
* Rendr routes are attached with `app.get()`, which adds them to the
* `app.router` middleware.
*/
app.use(app.router);
/**
* Error handler goes last.
*/
app.use(mw.errorHandler());
}
/**
* Initialize our Rendr server.
*
* We can pass inject various modules and options here to override
* default behavior:
* - dataAdapter
* - errorHandler
* - notFoundHandler
* - appData
*/
function initServer() {
var options = {
dataAdapter: new DataAdapter(env.current.api),
errorHandler: mw.errorHandler(),
appData: env.current.rendrApp
};
server = rendr.createServer(app, options);
}
/**
* Start the Express server.
*/
function start() {
var port = process.env.PORT || 3030;
app.listen(port);
console.log("server pid %s listening on port %s in %s mode",
process.pid,
port,
app.settings.env);
}
/**
* Here we actually initialize everything and start the Express server.
*
* We have to add the middleware before we initialize the server, otherwise
* the 404 handler gets too greedy, and intercepts i.e. static assets.
*/
initMiddleware();
initServer();
// Only start server if this script is executed, not if it's require()'d
if (require.main === module) {
start();
}
exports.app = app;