-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (32 loc) · 1.09 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
const express = require('express')
const app = express()
require('dotenv').config()
const morgan = require('morgan')
const mongoose = require('mongoose')
const { expressjwt } = require('express-jwt')
// ... other imports
const path = require("path")
app.use(express.json())
app.use(morgan('dev'))
mongoose.connect(
`mongodb+srv://${process.env.DBUSER}:${process.env.DBPASS}@cluster0.k5floph.mongodb.net/`,
() => console.log('Connected to the DB')
)
// ... other app.use middleware
app.use(express.static(path.join(__dirname, "client", "build")))
app.use('/auth', require('./routes/authRouter.js'))
app.use('/api', expressjwt({ secret: process.env.SECRET, algorithms: ['HS256']})) // req.user
app.use('/api/parties', require('./routes/partyRouter.js'))
app.use((err, req, res, next) => {
console.log(err)
if(err.name === "UnauthorizedError"){
res.status(err.status)
}
return res.send({errMsg: err.message})
})
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
app.listen(9000, () => {
console.log(`Server is running on local port 9000`)
})