-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (39 loc) · 1.5 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
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const bodyParser = require("body-parser");
const path = require('path');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
let uri = process.env.LOCAL_URI;
if (process.env.NODE_ENV === 'production') {
uri = process.env.ATLAS_URI;
}
// TODO: Figure out what all these options are and if they're needed.
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true, useFindAndModify: false });
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
})
const playersRouter = require('./routes/players');
const offeringsRouter = require('./routes/offerings');
const wishlistsRouter = require('./routes/wishlists');
const seedsRouter = require('./routes/seeds');
// Creates API routes for our use: http://localhost:5000
app.use('/players', playersRouter);
app.use('/offerings', offeringsRouter);
app.use('/wishlists', wishlistsRouter);
app.use('/seeds', seedsRouter);
if (process.env.NODE_ENV === 'production') {
app.use(express.static( 'client/build' ));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html')); // relative path
});
}
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});