-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (31 loc) · 1.16 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
33
34
35
36
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 morgan = require('morgan')
const path = require('path')
app.use(morgan('dev'))
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('/users', require('./routes/userRoutes'))
app.use('/services', require('./routes/serviceRoutes'))
app.use(express.static(path.join(__dirname, 'client', '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:27017/users', {useNewUrlParser: true}).then(() => {
console.log('Connected to MongoDB')
}).catch(err => console.log(err))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
})
app.listen(port, () => {
console.log(`server is running on port ${port}`)
})