-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (68 loc) · 1.84 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
'use strict';
// External libraries import
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const { mongoose } = require('./db/mongoose');
mongoose.set('useFindAndModify', false);
// Routes import
const auth = require('./api/auth');
const admin = require('./api/admin');
const shopper = require('./api/shopper');
const store = require('./api/store');
const queue = require('./api/queue');
// Create main express app
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Create a session cookie
app.use(session({
secret: 'oursecret',
resave: false,
saveUninitialized: false,
cookie: {
expires: 600000,
httpOnly: true
}
}));
// Use express routers
app.use('/', auth);
app.use('/', admin);
app.use('/', shopper);
app.use('/', store);
app.use('/', queue);
/*** Webpage routes below **********************************/
// Serve the build
app.use(express.static(__dirname + '/client/build'));
// All routes other than above will go to index.html
app.get('*', (req, res) => {
// check for page routes that we expect in the frontend to provide correct status code.
const goodPageRoutes = [
'/',
'/map',
'/queue',
'/profile',
'/store/profile',
'/admin/profile',
'/admin/store/queues',
'/admin/shopper/queues',
'/admin/messages',
'/store/queues',
'/store/shoppers',
'/login',
'/register',
'/logout',
'/store/:username'
];
if (!goodPageRoutes.includes(req.url)) {
res.status(404);
}
// send index.html
res.sendFile(__dirname + '/client/build/index.html');
});
/*************************************************/
// Express server listening...
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});