mirror of
https://github.com/Lissy93/web-check.git
synced 2025-02-15 18:11:24 +01:00
34 lines
824 B
JavaScript
34 lines
824 B
JavaScript
const axios = require('axios');
|
|
const xml2js = require('xml2js');
|
|
const middleware = require('./_common/middleware');
|
|
|
|
const fetchSitemapHandler = async (url) => {
|
|
let sitemapUrl;
|
|
|
|
try {
|
|
// Fetch robots.txt
|
|
const robotsRes = await axios.get(`${url}/robots.txt`);
|
|
const robotsTxt = robotsRes.data.split('\n');
|
|
|
|
for (let line of robotsTxt) {
|
|
if (line.startsWith('Sitemap:')) {
|
|
sitemapUrl = line.split(' ')[1];
|
|
}
|
|
}
|
|
|
|
if (!sitemapUrl) {
|
|
throw new Error('Sitemap not found in robots.txt');
|
|
}
|
|
|
|
// Fetch sitemap
|
|
const sitemapRes = await axios.get(sitemapUrl);
|
|
const sitemap = await xml2js.parseStringPromise(sitemapRes.data);
|
|
|
|
return sitemap;
|
|
} catch (error) {
|
|
throw new Error(error.message);
|
|
}
|
|
};
|
|
|
|
exports.handler = middleware(fetchSitemapHandler);
|