Add a timeout of 2 seconds for the ping module

This commit is contained in:
ajnart
2022-12-11 14:25:27 +09:00
parent 2bf01990fe
commit 81bc1f8f0c

View File

@@ -7,13 +7,15 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
const { url } = req.query;
const agent = new https.Agent({ rejectUnauthorized: false });
await axios
.get(url as string, { httpsAgent: agent })
.get(url as string, { httpsAgent: agent, timeout: 2000 })
.then((response) => {
res.status(response.status).json(response.statusText);
})
.catch((error) => {
if (error.response) {
res.status(error.response.status).json(error.response.statusText);
} else if (error.code === 'ECONNABORTED') {
res.status(408).json('Request Timeout');
} else {
res.status(500).json('Server Error');
}