-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprolific.js
61 lines (53 loc) · 1.12 KB
/
prolific.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
var express = require('express');
var glenlivet = require('../index');
var app = express();
var prolific = glenlivet.createBarrel({
plugins: [
require('glenlivet-request'),
require('glenlivet-htmltojson')
],
pluginDefaults: {
request: {
protocol: 'http',
host: 'www.prolificinteractive.com'
}
}
});
prolific.bottle('getLinks', {
request: {
pathname: function (data) {
return '/' + (data.page || '');
}
},
htmlToJson: ['a[href]', {
'text': function ($a) {
return $a.text();
},
'href': function ($a) {
return $a.attr('href');
}
}]
});
app.use(function addResponseMethods (req, resp, next) {
resp.success = function (data) {
resp.send(data.json);
};
resp.error = function (err) {
resp
.status(err.status)
.send({
error: {
type: err.type,
message: err.message
}
});
};
next();
});
app.listen(process.env.PORT || 8888);
app.get('/:page/links', function (req, resp) {
var page = req.param('page');
prolific
.getLinks({ page: page })
.done(resp.success, resp.failure);
});