Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
78 lines (68 loc) · 2.58 KB
/
Copy pathserver.js
File metadata and controls
78 lines (68 loc) · 2.58 KB
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
import initializeDatabase from './src/server/database/initializeDatabase';
import express from 'express';
import bodyParser from 'body-parser';
import path from 'path';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import compress from 'compression';
import 'colors';
import opn from 'opn';
import request from 'request';
const app = express();
const isProduction = process.env.NODE_ENV === 'production';
const port = process.env.PORT || 9000;
process.on('uncaughtException', function (error) {
console.error('Uncaught error : ', error);
});
(async function () {
app.disable('etag');
app.use(compress());
app.use(bodyParser.json());
await initializeDatabase();
require('./src/server/routes.js')(app);
if (isProduction) {
app.use(express.static('./build'));
} else {
console.info('adding webpack');
const webpackConfig = require('./webpack.config.babel');
const compiler = webpack(webpackConfig);
const middleware = [
webpackDevMiddleware(compiler, {
publicPath: '/', // Same as `output.publicPath` in most cases.
contentBase: path.join(__dirname, 'src', 'client'),
hot: true,
stats: {
colors: true,
},
}),
webpackHotMiddleware(compiler, {
log: console.log, // eslint-disable-line no-console
heartbeat: 2000,
path: '/__webpack_hmr',
}),
];
app.use(middleware);
}
app.use(function pushstatehook (req, res, next) {
var ext = path.extname(req.url);
console.info(ext, req.url);
if ((ext === '' || ext === '.html') && req.url !== '/') {
console.info('returning the index.html here');
console.info(req.url);
console.info(`http://localhost:${port}/`);
req.pipe(request(`http://localhost:${port}/`)).pipe(res);
} else {
next();
}
});
app.listen(port, function () {
console.log('╔═══════════════════════════════════════════════════════════'.green.bold);
console.log('║ Background Geolocation Server | port: %s'.green.bold, port);
console.log('╚═══════════════════════════════════════════════════════════'.green.bold);
// Spawning dedicated process on opened port.. only if not deployed on heroku
if (!process.env.DYNO) {
opn(`http://localhost:${port}`);
}
});
})();