-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
167 lines (131 loc) · 4.83 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var favicon = require('serve-favicon');
var path = require("path");
var url = require("url");
var http = require('http');
var bodyParser = require('body-parser');
var app = express();
var searchName;
var pageLinks = [];
var allowed = [];
var disallowed = [];
var robots = 'robots.txt';
var scraped = [];
app.use(favicon(path.join(__dirname+'/favicon.ico')));
app.set('view engine', 'html');
app.use(bodyParser()); //reads a form's input and stores it as a javascript object accessible through `req.body`
var getLinks = function(node) {
var $ = cheerio.load(node);
var links = $('a');
$(links).each(function(i, link){
var item = $(link).attr('href');
if (item !== undefined) {
if (item != '/') {
item = item.replace(/\/$/, "");
}
item = item.split("#")[0];
if ((item.indexOf(searchName) == 0 ) || (item.indexOf('/') == 0)) {
if (pageLinks.indexOf(item) === -1) {
pageLinks.push(item);
}
}
}
});
pageLinks.sort();
}
var addScrapes = function(links) {
var pageLinksLength = links.length;
for (var i = 0; i < pageLinksLength; i++) {
var siteLink = pageLinks[i];
request(pageLinks[i], function(error, response, html){
if(!error){
scraped.push({'path': this.path})
} else {
res.status(500)
res.render('error', { error: error })
}
});
}
}
app.get('/', function(req, res, next){
var html = '<!DOCTYPE html><html><head><title>Simple Web Crawler</title></head><body>' +
'<h1>Simple Web Crawler</h1>' +
'<p>This is a simple webcrawler that will scrapes your target url and returns a list of links or simple site map.</p>' +
'<form action="/" method="post">' +
'<label for"searchName">Target url:</label> ' +
'<input type="url" name="searchName" placeholder="http://your-address-here.com" />' +
'<button type="submit">Submit</button>' +
'</form>'+
'</body></html>';
res.send(html);
next();
});
// This route receives the posted form.
// As explained above, usage of 'body-parser' means
// that `req.body` will be filled in with the form elements
app.post('/', function(req, res, next){
searchName = req.body.searchName;
res.setHeader("Content-Type", "text/html");
res.write('<!DOCTYPE html><html><head><title>Simple Web Crawler</title></head><body>');
res.write('<p>Crawling ' + searchName + '</p>');
getContent(searchName)
.then((html) => {
getLinks(html);
var itemsLength = pageLinks.length;
if (itemsLength > 0 ) {
res.write('<h3>Number of links scraped:'+itemsLength+'</h3>');
res.write('<ol>');
cheerio(pageLinks).each(function(i, link){
if(link == searchName) {
res.write('<li><a href="'+link +'">/</a></li>');
}
else if ((link.indexOf(searchName) == 0 ) && (link != searchName)) {
var searchName2Length = searchName.length;
link = link.substring(searchName2Length);
res.write('<li><a href="'+searchName +'">'+link+'</a></li>');
} else {
res.write('<li><a href="'+searchName+ link +'">'+link+'</a></li>');
}
})
res.write('</ol>');
} else {
res.write('<h3>No results returned from '+searchName+'</h3>');
}
res.write('<a href="/">Try again.</a>');
res.write('</body></html>');
})
.then((html) => {
//reset
pageLinks = [];
res.end();
})
.catch((err) => console.error(err));
next();
});
const getContent = function(url) {
// return new pending promise
return new Promise((resolve, reject) => {
// select http or https module, depending on reqested url
var lib = url.startsWith('https') ? require('https') : require('http');
var request = lib.get(url, (response) => {
// handle http errors
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
// temporary data holder
var body = [];
// on every content chunk, push it to the data array
response.on('data', (chunk) => body.push(chunk));
// we are done, resolve promise with those joined chunks
response.on('end', () => resolve(body.join('')));
});
// handle connection errors of the request
request.on('error', (err) => reject(err));
})
};
app.listen('8081')
console.log('Magic happens on port 8081');
exports = module.exports = app;