Ping module rework

This commit is contained in:
ajnart
2022-12-01 00:54:35 +09:00
parent 2bf01990fe
commit 2354399117
3 changed files with 42 additions and 19 deletions

View File

@@ -1,26 +1,21 @@
import axios from 'axios';
import https from 'https';
import ping from 'ping';
import { NextApiRequest, NextApiResponse } from 'next';
async function Get(req: NextApiRequest, res: NextApiResponse) {
// Parse req.body as a ServiceItem
const { url } = req.query;
const agent = new https.Agent({ rejectUnauthorized: false });
await axios
.get(url as string, { httpsAgent: agent })
.then((response) => {
res.status(response.status).json(response.statusText);
})
.catch((error) => {
if (error.response) {
res.status(error.response.status).json(error.response.statusText);
} else {
res.status(500).json('Server Error');
}
});
// // Make a request to the URL
// const response = await axios.get(url);
// // Return the response
// Parse url as URL object
const parsedUrl = new URL(url as string);
// Ping the URL
const response = await ping.promise.probe(parsedUrl.hostname, {
timeout: 1,
});
// Return 200 if the alive property is true
if (response.alive) {
return res.status(200).end();
}
// Return 404 if the alive property is false
return res.status(404).end();
}
export default async (req: NextApiRequest, res: NextApiResponse) => {