-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
85 lines (78 loc) · 2.22 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const logger = require('morgan')
const authRouter = require('./routers/authRouter')
const passport = require('passport')
const appRouter = require('./routers/appRouter')
const { authorized } = require('./auth/auth')
const { User, Coin } = require("./models");
const path = require('path')
const PORT = process.env.PORT || 4567
// initializing the express app
const app = express()
app.use(express.static(path.join(__dirname, './client/build')));
// configure middleware
app.use(logger('dev'))
app.use(cors())
// parse application/x-www-form-urlencoded
// for easier testing with Postman or plain HTML forms
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json())
app.use('/auth', authRouter)
app.use('/app', authorized, appRouter)
app.use(passport.initialize())
app.get('/', async (req, res) => {
try {
res.json({ message: 'Crypto working' })
} catch (e) {
res.status(e.status).json({ message: e.status })
}
})
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.json({ message: err.message })
})
app.post(`/dashboard/:userId`, async( req, res)=>{
try {
const { userId } = req.params
const user = await User.findByPk(userId)
const newFav =await Coin.create(req.body)
await user.addCoin(newFav)
res.send(newFav)
} catch (error) {
throw error
}
})
app.get(`/dashboard/:userId`, async (req, res)=>{
try {
const { userId } = req.params
const user= await User.findByPk(userId,{
include: [{
model: Coin,
through: 'user_coins'
}]
} )
res.send(user)
} catch (error) {
throw error
}
}
)
app.put(`/dashboard/:userId/:coin`, async (req, res) => {
try {
const { userId, coin } = req.params
const user = await User.findByPk(userId)
const unFav = await Coin.findAll({where:[{name:coin}]})
await user.removeCoin(unFav)
res.send(unFav)
} catch (error) {
throw error
}
})
app.listen(PORT, () => console.log(`App is up and running listening on port ${PORT}`))
if (process.env.NODE_ENV == "production") {
app.use('*', (req, res) => res.sendFile(path.join(__dirname, './client/build', "index.html")));
}