-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakeApi.js
77 lines (69 loc) · 2.16 KB
/
fakeApi.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
/**
* @file
* Provides api calls to vailidate a new
*/
const express = require('express')
const server = require('./serverLogic')
const app = express()
const blockchain = require('./blockchain')
const bodyParser = require('body-parser')
app.use(bodyParser.json()) // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
// KEYS
const kVote = 'vote'
const kNews = 'newsURL'
const kPublicKey = 'userPublicKey'
const kAesKey = 'aesKey'
const kCreateBlock = 'createBlock'
const kTrendingNews = 'trendingNews'
const encryptedVote = 'encryptedVote'
// Fake API Endpoints
const voteForNewsPath = `/${kVote}`
const verifyNewsPath = `/${kNews}/:${kNews}`
const createBlockPath = `/${kCreateBlock}`
const trendingNewsPath = `/${kTrendingNews}`
// Api calls
// Allows getting trending news
app.get(trendingNewsPath, function(req, res) {
server.getTrendingNews().then(function(result) {
res.send(result)
}, function(error) {
console.log(error)
res.send(error)
})
})
// Allows voting for a news by passing a vote(bool) and a news(url)
app.post(voteForNewsPath, function(req, res) {
var encryptedVote = req.body.encryptedVote
var userPublicKey = req.body.userPublicKey
server.addVote(encryptedVote, userPublicKey).then(function(result) {
res.sendStatus(200)
}).catch(function(error) {
res.status(500).send({errorCode: error.statusMessage})
})
})
// Allows verifying a new
app.get(verifyNewsPath, function (req, res) {
console.log("Ask to verify news")
news = req.params[kNews]
console.log(news)
server.verifyNews(news).then(function(result) {
res.send(result)
}, function(error) {
console.log(error)
res.send(error)
})
})
// Allows adding a new block
app.post(createBlockPath, function(req, res) {
console.log("Received a block creation request")
const pubKey = req.body.userPublicKey
blockchain.createBlock(pubKey).then(function(result) {
res.send(result)
}).catch(function(error) {
console.log(error)
res.send(error)
})
})
// RUN SERVER
app.listen(3000, () => console.log('Listening on port 3000!'))