-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (34 loc) · 1.25 KB
/
server.js
File metadata and controls
46 lines (34 loc) · 1.25 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
/**
* Created by asistent on 06.05.2016.
*/
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
var PORT = 8000;
var string = [];
request('https://geekbrains.ru/posts', function (error, response, html) {
if (error) {
return console.error('error is: ', error);
}
if (response.statusCode !== 200) {
return console.log('incorrect statusCode: ', response.statusCode);
}
var $ = cheerio.load(html);
$('div.col-sm-6.col-md-4.col-xs-6.event.search_row').each(function (i, element) {
$(element).find($('.text-dark')).parent('a').attr('target','_blank');
string[i] = $(element).find($('.img_preview')) + '<br>' +
$(element).find($('.text-dark')).parent('a') + '<br>' +
$(element).find($('div.m-t-sm')).next() + '<br>';
return (i !== 10);
});
string = string.join('').replace(new RegExp("/posts/",'g'), "https://geekbrains.ru/posts/");
});
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
response.write(string);
response.end();
}).listen(PORT, function () {
console.log("Let's get started: http://localhost:" + PORT);
});