Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
75 lines (62 loc) · 1.78 KB
/
Copy pathapp.js
File metadata and controls
75 lines (62 loc) · 1.78 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
var util = require('util'),
rss = require('parserss'),
express = require('express'),
md = require('to-markdown'),
app = express();
var svbtle_url = 'https://%s.svbtle.com/feed';
function parseArticle (article) {
var title = article.title,
content = article.description,
markdown = md(content),
author = article.author,
timestamp = article.date.valueOf(),
url = article.link;
return {
title: title,
content: content,
markdown: markdown,
author: author,
timestamp: timestamp,
url: url
};
}
function getRSSFeed (username, limit, callback) {
var url = util.format(svbtle_url, username);
rss(url, limit, function (err, data) {
callback(err, data);
});
}
function handleError (res, err, data) {
if (err) {
res.status(500).send();
return true;
}
if (data.articles && data.articles.length < 1) {
res.status(404).send();
return true;
}
}
app.use(function (req, res, next) {
res.set('Access-Control-Allow-Origin', '*');
next();
});
app.get('/:username/latest', function (req, res) {
getRSSFeed(req.params.username, 1, function (err, data) {
if (handleError(res, err, data)) return;
res.json(parseArticle(data.articles[0]));
});
});
app.get('/:username/latest/:number', function (req, res) {
if (('' + parseFloat(req.params.number)) !== req.params.number) {
res.status(400).send();
return;
}
getRSSFeed(req.params.username, +req.params.number, function (err, data) {
if (handleError(res, err, data)) return;
res.json(data.articles.map(parseArticle));
});
});
app.get('/ping', function (req, res) {
res.send('pong');
});
app.listen(process.env.PORT);