-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·120 lines (93 loc) · 3.9 KB
/
app.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var app = express();
// bodyParser.urlencoded解析form表单提交的数据
app.use(bodyParser.urlencoded({extended: false}));
// bodyParser.json解析json数据格式的
app.use(bodyParser.json());
/**
网易云跟帖回推接口
每次有新评论,网易会将评论数据回推至此接口
接口中会对最新评论数据按照用户名称分文件进行保存
*/
app.post('/saveComments',function(req, res){
// 对象转换为字符串
var str_json = JSON.stringify(req.body.data);
// 将对象数组最外层的引号及方括号删掉
str_json = str_json.substr(0,str_json.length-2).substr(2,str_json.length);
// 获取用户名
var user_name=req.query.user;
var filePathName = 'data/'+user_name+'.json'
fs.exists(filePathName, function(exists) {
if (exists) {
// 文件中将评论对象以逗号分隔存储,取回时两边添加方括号即为对象数组
fs.appendFile(filePathName, ','+str_json, function (err) {
// console.log("追加文件");
});
}else{
fs.writeFile(filePathName, str_json, 'utf8', function(){
// console.log("新增文件");
});
}
});
res.end("ok");
});
/**
获取最新跟帖接口(返回html)
按照用户名返回最新评论数据,该接口已将数据拼装为html直接返回,可直接使用于Maupassant主题的展示
*/
app.get('/getComments', function(req, res){
var user_name=req.query.user;
var filePathName = 'data/'+user_name+'.json'
fs.readFile(filePathName,'utf8',function (err, data) {
if(err) console.log(err);
var json_str = '['+data+']';
try {
json_str = json_str.replace(/\\/g,"");
json = JSON.parse(json_str);
var comment_html = "";
var length = json.length > 5 ? json.length : 5;
for(var i=json.length-1; i>=length-5; i--){
var article_title = json[i].title;
var article_url = json[i].url;
var comments = json[i].comments;
var comment_time = comments[0].ctime;
var now = new Date(comment_time);
var month=now.getMonth()+1;
var date=now.getDate();
comment_time = month+"月"+date+"日";
var comment_user = comments[0].user.nickname;
var comment_content = comments[0].content;
comment_html = comment_html+'<li class="ds-comment"><div class="ds-meta"><a rel="nofollow author" target="_blank" href="">'+comment_user+' </a><span class="ds-time">'+comment_time+'</span></div><div class="ds-thread-title">在 <a href="'+article_url+'#comments">'+article_title+'</a> 中评论</div><div class="ds-excerpt">'+comment_content+'</div></li>';
}
res.header('Access-Control-Allow-Origin', '*');
res.send({"status":"ok","content":comment_html});
} catch (err) {
json = null;
res.send({"status":"error"});
}
});
});
/**
获取最新跟帖接口(json格式)
按照用户名返回最新评论的json格式数据,便于用户自定义展示样式
*/
app.get('/getRawComments', function(req, res){
var user_name=req.query.user;
var filePathName = 'data/'+user_name+'.json'
fs.readFile(filePathName,'utf8',function (err, data) {
if(err) console.log(err);
var json_str = '['+data+']';
try {
json_str = json_str.replace(/\\/g,"");
json = JSON.parse(json_str);
res.header('Access-Control-Allow-Origin', '*');
res.send({"status":"ok","content":json});
} catch (err) {
json = null;
res.send({"status":"error"});
}
});
});
app.listen(3001);