-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (45 loc) · 1.3 KB
/
index.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
const url = require('url');
const sitemaps = require('sitemap-stream-parser');
var domains = [];
exports.getAllURLs = function(req, res){
// Okay... First, it's assumed that site is defined somehow.
var url;
if(req.body.site){
url = parseUrl(req.body.site);
}else if(req.query && req.query.site){
url = parseUrl(req.query.site);
}else{
return res.status( 420 ).send( 'Missing required \'site\' parameter.' );
}
// Now that we have url...
sitemaps.parseSitemaps(url.protocol + '//' + url.host + '/sitemap.xml', addURL, function(err, sitemaps){
if(err){
res.status(500).send('We are unable to process your request right now.');
}else{
res.status(200).send({
sitemaps: sitemaps,
domains: domains
});
}
});
// res.status( 201 ).send();
}
function addURL(url){
if( ! domains.includes(url) ){
domains.push(url);
}
}
function parseUrl(site){
// First, validate that site is at minimum a domain.
var urlToParse = url.parse(site);
// If host isn't defined, check path.
if(!urlToParse.host){
// It's distinctly possible that we were merely missing http...
if(site.indexOf('http') == -1){
// Found it. Try again.
return parseUrl('http://' + site);
}
// Otherwise... hm. That's not good
}
return urlToParse;
}