-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·61 lines (50 loc) · 1.47 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
// Import libraries
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const PORT = process.env.PORT || 5000;
// Initialize App
const app = express();
app.set('port', (process.env.PORT || 5000));
app.use(cors());
app.use(bodyParser.json());
// Initialize routes
const router = require('./routes/api');
app.use('/api', router);
// Connect to mongodb through mongoose
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
require('dotenv').config();
const url = process.env.MONGODB_URI;
mongoose.connect(url, {useNewUrlParser: true, useCreateIndex:true, useUnifiedTopology: true});
// Do stuff
app.use((req, res, next) =>
{
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
);
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, PATCH, DELETE, OPTIONS'
);
next();
});
///////////////////////////////////////////////////
// For Heroku deployment
// Server static assets if in production
if (process.env.NODE_ENV === 'production')
{
// Set static folder
app.use(express.static('frontend/build'));
app.get('*', (req, res) =>
{
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
});
}
app.listen(PORT, () =>
{
console.log(`Server listening on port ${PORT}.`);
});