This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
executable file
·139 lines (102 loc) · 3.24 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
131
132
133
134
135
136
137
138
139
import express from 'express';
import webpack from 'webpack';
import WebpackMiddleware from 'webpack-dev-middleware';
import WebpackHotMiddleware from 'webpack-hot-middleware';
import http from 'http';
import fs from 'fs'
var log4js =require('log4js')
// import React from "react"
// import ReactDOMServer from 'react-dom/server'
// import App from './src/components/App.jsx'
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('./logs/server.log'), 'server');
var logger = log4js.getLogger('server')
logger.setLevel('INFO')
// logger.trace();
// logger.debug();
// logger.info();
// logger.warn();
// logger.error();
// logger.fatal();
var server = function() {
var self = this;
self.setupVariables = function() {
self.port = process.env.PORT || 3000
self.webpackConfig = require(process.env.WEBPACK_CONFIG ? process.env.WEBPACK_CONFIG : './webpack.config');
self.compiler = webpack(self.webpackConfig);
};
self.populateCache = function() {
if (typeof self.pCache === "undefined") {
self.pCache = {
'index.ejs': ''
};
}
// Local cache for static content.
self.pCache['index.ejs'] = fs.readFileSync('./views/index.ejs');
};
self.cacheGet = function(key) {
return self.pCache[key];
};
self.terminator = function(sig) {
if (typeof sig === "string") {
logger.info('%s: Received %s - terminating sample app ...',
Date(Date.now()), sig);
process.exit(1);
}
logger.info('%s: Node server stopped.', Date(Date.now()));
}
self.setupTerminationHandlers = function() {
// Process on exit and signals.
process.on('exit', function() {
self.terminator();
});
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(function(element, index, array) {
process.on(element, function() {
self.terminator(element);
});
});
};
self.createRoutes = function() {
self.routes = {};
self.routes['/'] = function(req, res) {
const reactHtml = "You are looking nice today"
// place holder for future isomorphic rendering
//ReactDOMServer.renderToString(<App />);
res.render('index.ejs', {reactOutput: reactHtml});
};
};
self.initializeServer = function() {
self.createRoutes();
self.app = express();
self.app.use(express.static('public'));
self.app.use((WebpackMiddleware)(self.compiler, {
log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000
}));
self.app.use((WebpackHotMiddleware)(self.compiler, {
log: console.log
}));
self.app.set('view engine', 'ejs');
// Add handlers for the app (from the routes).
for (var r in self.routes) {
self.app.get(r, self.routes[r]);
}
};
self.initialize = function() {
self.setupVariables();
self.populateCache();
self.setupTerminationHandlers();
self.initializeServer();
};
self.start = function() {
// Start the app on the specific interface (and port).
self.app.listen(self.port, function() {
logger.info('%s: Node server started on %d ...',
Date(Date.now()), self.port);
});
};
}
let app = new server();
app.initialize();
app.start();