-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (64 loc) · 1.86 KB
/
Copy pathserver.js
File metadata and controls
79 lines (64 loc) · 1.86 KB
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
const { PORT } = require('./config.js')
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const Fan = require('./db/models/fanModel')
const db = require('./db')
const app = express()
// MIDDLEWARE
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(cors({
methods: ['POST', 'GET', 'PUT', 'DELETE'],
allowedHeaders: [
'Access-Control-Allow-Origin',
'Content-Type',
'x-access-token'
]
}))
// ROUTES
app.get('/get-fan', async (req, res) => {
try {
const uri = req.query.uri
const result = await Fan.findOne({ uri }).exec()
return res.json(result)
} catch (error) {
console.error(error)
return res.json(error)
}
})
app.get('/get-paginated-fans', async (req, res) => {
try {
const { start, end } = req.query
const allFans = await Fan.find({})
const selectedFans = allFans.slice(start, end)
return res.json(selectedFans)
} catch (error) {
console.error(error)
return res.json(error)
}
})
app.put("/put-fan", async (req, res) => {
try {
const { payload } = req.body
const { uri, owner } = payload
const result = await Fan.findOneAndUpdate(
{ uri },
{ $set: { owner: owner }}
).exec()
return res.json(result)
} catch (error) {
console.error(error)
return res.json(error)
}
})
app.listen(process.env.PORT, () => {
console.log('Listening on port ' + process.env.PORT)
})
// TEST PAYLOAD
// {
// "payload" = {
// "uri": "https://bafybeiad4cjnorjvotfqx737c2aha2fuvg2rfo6bznio22vbw6iyuyqs7y.ipfs.nftstorage.link/0000000000000000000000000000000000000000000000000000000000000001.json",
// "owner": "0xEB1e4Ff457D42B795edF00333Bb1d519fb423a72"
// }
// }