-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (27 loc) · 1.01 KB
/
index.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
const express = require('express');
const app = express();
require('dotenv').config();
const port = process.env.PORT || 5000;
const mongoose = require('mongoose');
const expressJwt = require('express-jwt');
const path = require('path');
app.use(express.json());
app.use('/api', expressJwt({secret: process.env.SECRET}));
app.use('/api', require('./routes/apiROutes'));
app.use('/auth', require('./routes/authRoutes'));
app.use(express.static(path.join(__dirname, 'cient', 'build')));
app.use((err, req, res, next) => {
if(err.name === 'UnauthorizedError'){
res.status(err.status);
}
return res.send({message: err.message})
});
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27107/triviaUsers', {useNewUrlParser: true}).then(() => {
console.log('Connection to MongoDB')
}).catch(err => console.log(err));
app.length('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
});
app.listen(port, () => {
console.log(`server is running on port ${port}`)
})