-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (53 loc) · 1.7 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
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
//connect DB
const config = require('config')
const db = config.get('mongoURI')
const connectDB = async () => {
try {
await mongoose.connect(db, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
});
console.log("MongoDB connected.");
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
connectDB();
// router
const locationRouter = require('./routes/locations');
const characterRouter = require('./routes/characters');
const desireRouter = require('./routes/desires');
const occupationRouter = require('./routes/occupations');
const themeRouter = require('./routes/themes');
const worldRouter = require('./routes/worlds');
const generateRouter = require('./routes/generate');
const outlinesRouter = require('./routes/outlines');
app.use('/locations', locationRouter);
app.use('/characters', characterRouter);
app.use('/desires', desireRouter);
app.use('/occupations', occupationRouter);
app.use('/themes', themeRouter);
app.use('/worlds', worldRouter);
app.use('/generate', generateRouter);
app.use('/outlines', outlinesRouter);
// router
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`)
})