mirror of
https://github.com/Lissy93/web-check.git
synced 2024-11-23 08:45:07 +01:00
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
const fetch = require('node-fetch');
|
|
|
|
exports.handler = async function(event, context) {
|
|
const siteURL = event.queryStringParameters.url;
|
|
|
|
if (!siteURL) {
|
|
return {
|
|
statusCode: 400,
|
|
body: JSON.stringify({ error: 'Missing url query parameter' }),
|
|
};
|
|
}
|
|
|
|
let parsedURL;
|
|
try {
|
|
parsedURL = new URL(siteURL);
|
|
} catch (error) {
|
|
return {
|
|
statusCode: 400,
|
|
body: JSON.stringify({ error: 'Invalid url query parameter' }),
|
|
};
|
|
}
|
|
|
|
const robotsURL = `${parsedURL.protocol}//${parsedURL.hostname}/robots.txt`;
|
|
|
|
try {
|
|
const response = await fetch(robotsURL);
|
|
const text = await response.text();
|
|
|
|
if (response.ok) {
|
|
return {
|
|
statusCode: 200,
|
|
body: text,
|
|
};
|
|
} else {
|
|
return {
|
|
statusCode: response.status,
|
|
body: JSON.stringify({ error: 'Failed to fetch robots.txt', statusCode: response.status }),
|
|
};
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
statusCode: 500,
|
|
body: JSON.stringify({ error: `Error fetching robots.txt: ${error.message}` }),
|
|
};
|
|
}
|
|
};
|