如何配置 Next.JS API 超时

que*_*nsw 11 javascript reactjs next.js vercel

由于另一个 API 的一些限制,我的 API 在 10 分钟后返回结果。在 localhost 中,我可以等待 10 分钟并成功接收数据,但在 Vercel 中,我的 API 返回 504,这意味着超时错误。我怎样才能禁用它?

在此输入图像描述

小智 0

您可以像这样增加请求超时:

// pages/api/someRoute.js
import axios from 'axios';

export default async (req, res) => {
  try {
    const response = await axios.get('https://api.example.com/someData', {
      timeout: 5000, // Set your desired timeout value in milliseconds (e.g., 5000ms or 5 seconds)
    });

    const data = response.data;
    res.status(200).json({ data });
  } catch (error) {
    res.status(500).json({ error: 'Request timed out or an error occurred' });
  }
};
Run Code Online (Sandbox Code Playgroud)