/
bootstrap.js
69 lines (58 loc) · 1.91 KB
/
bootstrap.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
import express from 'express';
import hbs from 'express-handlebars';
import serveStatic from 'serve-static';
import router from './router';
class App{
constructor(){
// server config
this.hostname = process.env.HOST || 'localhost';
this.port = process.env.PORT || 2000;
// bind class methods
this.init = this.init.bind(this);
this.start = this.start.bind(this);
this.use = this.use.bind(this);
// Create express server instance & initialize
this.express = express();
this.init();
// Create application logging functionality.
// A neat console wrapper that keeps the correct line number.
// http://stackoverflow.com/questions/13815640/a-proper-wrapper-for-console-log-with-correct-line-number
/*eslint-disable no-console */
this.log = function() {
return console.log.bind(console);
}();
/*eslint-enable no-console */
}
init(){
// -> Set the folder for html views & disable powered-by header
this.express.engine('html', hbs({ extname: 'html' }));
this.express.set('views', process.cwd() + '/src/server/views');
this.express.set('view engine', 'html');
this.express.locals.settings['x-powered-by'] = false;
this.express.disable('x-powered-by');
}
// Launch express server with react-router
start(options={ message: null }){
// serve build folder
this.express.use(serveStatic('build'));
// Listen on specified port
this.express.listen(this.port, (error)=> {
if (error) {
this.express.error(error);
} else {
this.log('==> 🌎 Listening on http://' + this.hostname + ':%s/', this.port);
if (options.message){
this.log(options.message);
}
}
});
// start react-router
this.express.use(router);
}
// bind express middleware
use(...middlewareOptions){
this.express.use(...middlewareOptions);
}
}
// Create Node Global
GLOBAL.app = new App;