This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
59 lines (50 loc) · 1.58 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
const webpack = require('webpack');
const middleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const express = require('express');
const path = require('path');
const cookiesMiddleware = require('universal-cookie-express');
const { nanoid } = require('nanoid');
const db = require('./db');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(cookiesMiddleware())
.use(function (req, res, next) {
// get the user cookies using universal-cookie
const session = req.universalCookies.get('session');
if (session === undefined) {
req.universalCookies.set('session', nanoid(20), { path: '/' });
}
next();
});
const webpackConfig = require('./webpack.config');
const compiler = webpack(webpackConfig);
if (webpackConfig.mode === 'production') {
app.use(express.static(path.join(__dirname, 'dist')));
const routes = require('./routes');
app.use(routes);
} else {
app.use(middleware(compiler));
app.use(hotMiddleware(compiler));
require('node-hot')
.configure({
exclude: [
/[/\\]node_modules[/\\]/,
],
});
app.use((req, res, next) => {
require('./routes').handle(req, res, next);
});
}
const port = process.env.PORT || 3000;
// Now includes db connection test before starting web server
db.query("SELECT * from TEAMS", function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
});