-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (76 loc) · 2.31 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const bodyParser = require('body-parser')
const express = require('express')
const path = require('path')
const { API } = require('./api')
async function main() {
const app = express()
const port = 5000
const api = new API()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.listen(port)
console.log(`listening on port ${port}`)
await api.connectDB()
// SERVE STATIC BUILD IN PRODUCTION
// ALSO DISABLE PROXY TO PORT 5000
//app.use('/', express.static(path.join(__dirname, 'client/build')))
app.post('/api/getTweets', async (req, res) => {
const userId = req.body.userId
const response = await api.getTweets(userId)
await res.json(response)
})
app.post('/api/post', async (req, res) => {
const user = req.body.user
if(!user) {
return
}
const data = {
author: {
_id: req.body.user._id,
secret: req.body.user.secret
},
forId: req.body.forId,
type: req.body.type,
date: Date.now(),
content: req.body.content,
comments: [],
likes: 0,
retweets: 0,
liked_by: []
}
await api.postTweet(data, user)
})
app.post('/api/login', async (req, res) => {
const username = req.body.username
const password = req.body.password
const response = await api.login(username, password)
await res.send(response)
})
app.post('/api/getUserFromSecret', async (req, res) => {
const secret = req.body.secret
const response = await api.getUserFromSecret(secret)
await res.json(response)
})
app.post('/api/getUserFromId', async (req, res) => {
const _id = req.body._id
const response = await api.getUserFromId(_id)
await res.json(response)
})
app.post('/api/getUserFromHandle', async (req, res) => {
const userHandle = req.body.userHandle
const response = await api.getUserFromHandle(userHandle)
await res.json(response)
})
app.post('/api/likeTweet', async (req, res) => {
const user = req.body.user
const tweetId = req.body.tweetId
const response = await api.likeTweet(tweetId, user)
await res.json(response)
})
app.post('/api/getCommentCount', async (req, res) => {
const tweetId = req.body.tweetId
const response = await api.getCommentCount(tweetId)
await res.json(response)
})
}
main()