-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
61 lines (48 loc) · 1.48 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
var compression = require('compression');
var express = require('express');
var cors = require('cors');
var helmet = require('helmet');
var mongoose = require('mongoose');
var path = require('path');
require('dotenv').config();
// Create the express server
var app = express();
app.use(express.json());
app.use(helmet());
app.use(compression());
app.use(cors());
const URI = process.env.MONGODB_URI;
// Connect to MongoDB Atlas
mongoose
.connect(URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.then(() => console.log('MongoDB Atlas connection established successfully'))
.catch(err => console.log(err));
const users = require('./api');
users.setApp( app, mongoose );
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();
});
if (process.env.NODE_ENV === 'production') {
app.use(express.static('frontend/desktop/public/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend', 'desktop', 'public', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});