-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
103 lines (86 loc) · 2.5 KB
/
server.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
var qs = require('qs');
var url = require('phantom-url');
var webpage = require('webpage');
var webserver = require('webserver');
var urlencode = require('urlencode');
var system = require('system');
var args = system.args;
if (args.length !== 2) {
console.log('Usage: phantomjs server.js <port>');
phantom.exit(1);
}
startServer(args[1]);
function startServer(port) {
server = webserver.create();
var service = server.listen(port, requestHandler);
if (!service) {
console.log('FAIL to start web server');
phantom.exit(1);
} else {
console.log('http://localhost:' + port);
}
}
function requestHandler(req, res) {
var parsedUrl = url(req.url);
var query = parsedUrl.search ? qs.parse(parsedUrl.search.substr(1)) : {};
var fetchUrl = query.fetch_url;
if (!fetchUrl) {
end(res, 400, { message: 'No URL requested.' });
return;
}
fetchUrl = urlencode.decode(fetchUrl);
if (!/^https?:\/\//.test(fetchUrl)) {
end(res, 400, { message: 'Requested URL has a non-supported protocol, use http or https.' });
return;
}
fetch(fetchUrl, function (err, article) {
if (err) {
end(res, 500, { message: err.message });
return;
}
end(res, 200, { article: article });
});
}
function end(res, status, data) {
var body = JSON.stringify({
success: status === 200,
data: data
});
res.setHeader('content-type', 'application/json');
res.statusCode = status;
res.write(body);
res.close();
}
function fetch(url, cb) {
var page = webpage.create();
page.onError = function () {};
page.open(url, function (status) {
if (status !== 'success') {
cb(new Error('FAIL to load the address'));
return;
}
if (!page.injectJs('node_modules/readability/Readability.js')) {
cb(new Error('FAIL to include Readability.js'));
return;
}
var article = page.evaluate(function () {
var location = document.location;
var uri = {
spec: location.href,
host: location.host,
prePath: location.protocol + "//" + location.host,
scheme: location.protocol.substr(0, location.protocol.indexOf(":")),
pathBase: location.protocol + "//" + location.host + location.pathname.substr(0, location.pathname.lastIndexOf("/") + 1)
};
var article = new Readability(uri, document).parse();
return {
title: article.title,
content: article.content,
excerpt: article.excerpt,
length: article.length
};
});
cb(null, article);
page.close();
});
}