-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
93 lines (78 loc) · 2.22 KB
/
Copy pathserver.js
File metadata and controls
93 lines (78 loc) · 2.22 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
80
81
82
83
84
85
86
87
88
89
90
91
92
//Install express server
const express = require('express');
const path = require('path');
var bodyParser = require('body-parser');
const app = express();
app.set('port', process.env.PORT || 8080);
// Serve only the static files from the dist directory
app.use(express.static(__dirname + '/dist/RedditLike'));
app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({extended : false}));
// Declarations
var count = 0;
var topicList = [];
class Topic {
// constructor
constructor(title) {
this.title = title;
this.id = count++;
this.votes = 0;
}
// upvote or downvote
vote(num) {
this.votes += num;
}
}
app.get('/', function(req,res) {
console.log('homepage');
res.sendFile(path.join(__dirname+'/dist/RedditLike/index.html'));
});
// New topic request handler
app.post('/newtopic', function(req, res){
console.log('newtopic request received :');
console.log(req);
if (req.body.title) {
topicList.push(new Topic(req.body.title));
res.json({ status : true });
} else {
res.json({ status : false });
}
});
// Topic List request handler
app.get('/topics', function(req, res){
console.log('topics request received');
console.log(JSON.stringify(topicList));
let result = [];
if (topicList && topicList.length > 0) {
result = topicList.sort((a, b) => b.votes-a.votes);
if (result.length > 20) {
result = result.slice(0, 20);
}
}
console.log(result);
res.send(JSON.stringify({
length : result.length,
order : 'desc',
items : result
}));
});
// Vote request handler
app.get('/votes', function(req, res){
console.log('vote request received :');
console.log(req.query);
let tid = req.query.id;
if (!topicList || topicList.length == 0) {
res.json({status : false});
}
for (var temp of topicList) {
if (temp.id == tid) {
temp.vote(parseInt(req.query.type));
break;
}
}
res.json({id : tid, status : true});
})
// Start the app by listening on the default Heroku port
app.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});