-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (62 loc) · 2.14 KB
/
app.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
const express = require('express');
const path = require('path');
var genuuid = require('uuid').v4;
const session = require('express-session');
const MongoStore = require('connect-mongo');
const app = express();
//app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
const api = require('./server/api');
const list = require('./server/list');
const brand = require('./server/brand');
const db = require('./server/db');
//Configure .env
require('dotenv').config();
//Set port as process.env.PORT if it is present otherwise set it to 4000
const port = process.env.PORT || 4000;
//const port = 27017;
//Initiate connection with database
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME
}).then(() => {
//Handle /api with the api middleware
app.use('/api', session({
genid() {
return genuuid() // use UUIDs for session IDs
},
store: new MongoStore({ client: db.getClient() }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
}), api);
app.use('/list', session({
genid() {
return genuuid() // use UUIDs for session IDs
},
store: new MongoStore({ client: db.getClient() }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
}), list);
app.use('/brand', session({
genid() {
return genuuid() // use UUIDs for session IDs
},
store: new MongoStore({ client: db.getClient() }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
}), brand);
//Handle non-api routes with static build folder
app.use(express.static(path.join(__dirname, 'build')));
//Return index.html for routes not handled by build folder
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
//Start listening on port
app.listen(port, () => {
console.log(`Server listening at port: ${port}`);
});
});