-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
69 lines (63 loc) · 1.64 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
var express = require('express')
, rendr = require('rendr')
, compress = require('compression')
, bodyParser = require('body-parser')
, serveStatic = require('serve-static')
, logger = require('morgan')
, config = require('config')
, app = express();
/**
* Initialize Express middleware stack.
*/
app.use(compress());
app.use(serveStatic(__dirname + '/public'));
app.use(logger('combined'))
app.use(bodyParser.json());
/**
* Initialize our Rendr server.
*/
var server = rendr.createServer({
dataAdapterConfig: config.api,
errorHandler: function(err, req, res, next) {
if (err.status == 404) {
res.redirect('/404');
} else {
console.error(err.stack);
res.redirect('/503');
}
},
notFoundHandler: function(req, res) {
res.redirect('/404');
}
});
/**
* To mount Rendr, which owns its own Express instance for better encapsulation,
* simply add `server` as a middleware onto your Express app.
* This will add all of the routes defined in your `app/routes.js`.
* If you want to mount your Rendr app onto a path, you can do something like:
*
* app.use('/my_cool_app', server);
*/
server.configure(function(expressApp) {
app.use('/', expressApp);
});
/**
* 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.get('env')
);
}
/**
* Only start server if this script is executed, not if it's require()'d.
* This makes it easier to run integration tests on ephemeral ports.
*/
if (require.main === module) {
start();
}
exports.app = app;