-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
38 lines (32 loc) · 1.3 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
const express = require('express');
const passport = require('passport');
require('./passport');
const path = require('path');
const app = express();
const cors = require('cors');
const connectDB = require('./config/db');
// Connect Database
connectDB();
//Initialize Middlewares
app.use(passport.initialize());
app.use(cors()); //Initialize communication between front and back end server (separate origins)
app.use(express.json({ extended: false })); //To accept requests in json (raw body format in Postman)
app.use(express.urlencoded({ extended: true })); //To accept requests in URL Enconded format (easier in Postman)
//Routes definition: what is the response for each endpoint?
app.use('/cities', require('./routes/cities'));
app.use('/itineraries', require('./routes/itineraries'));
app.use('/activities', require('./routes/activities'));
app.use('/users', require('./routes/users'));
app.use('/comments', require('./routes/comments'));
//Serve static assets in production
if (process.env.NODE_ENV === 'production') {
//Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log('Server is running on port ' + port);
});