-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (28 loc) · 1 KB
/
Copy pathserver.js
File metadata and controls
32 lines (28 loc) · 1 KB
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
const exp = require('express');
const app = exp();
const db = require('mongoose');
const users = require('./backend/Users');
const contacts = require('./backend/Contacts');
const login = require('./backend/Login');
const config = require('config');
const path = require('path');
if (!config.get('jwtSecret')) throw new Error('Set env variable');
const url = config.get('mongoURI');
db.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
.then(console.log('Connected to Database'))
.catch(err => {
console.error(err.message);
});
app.use(exp.json());
app.use('/api/users', users);
app.use('/api/login', login);
app.use('/api/contacts', contacts);
//Setup production environment
if (process.env.NODE_ENV === 'production') {
app.use(exp.static('frontend/build'));
app.get('*', (req, res) =>
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'))
);
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`listening to port ${port}`));