Add a timeout of 2 seconds for the ping module
This commit is contained in:
@@ -7,13 +7,15 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const { url } = req.query;
|
const { url } = req.query;
|
||||||
const agent = new https.Agent({ rejectUnauthorized: false });
|
const agent = new https.Agent({ rejectUnauthorized: false });
|
||||||
await axios
|
await axios
|
||||||
.get(url as string, { httpsAgent: agent })
|
.get(url as string, { httpsAgent: agent, timeout: 2000 })
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
res.status(response.status).json(response.statusText);
|
res.status(response.status).json(response.statusText);
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
res.status(error.response.status).json(error.response.statusText);
|
res.status(error.response.status).json(error.response.statusText);
|
||||||
|
} else if (error.code === 'ECONNABORTED') {
|
||||||
|
res.status(408).json('Request Timeout');
|
||||||
} else {
|
} else {
|
||||||
res.status(500).json('Server Error');
|
res.status(500).json('Server Error');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user