0

我想从 Next.js 的 api 文件夹中调用一个外部 API。我想在服务器端调用它,因为客户端不应该看到调用了什么 URL。在开发环境中,我使用 Mirage 服务器来模拟外部 API。是否可以使用 MirageJS 捕获此请求?

更多细节:

这是从客户端到 API 服务器端的第一次调用。

async function userExists(email: string, callback: (arg: any) => void)  {
    await axios.get('/api/lcm/users/exists/' + email)
        .then(response => {
            logger.info(response)
            callback(response.status)
        })
        .catch(error => {
            logger.error("an error occured in lcmServices, userExists()", error)
    //do something with errors
            callback(error)
            throw error
        });
}

这个片段从上面调用。

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
    const  mail = req.query.email;

    await axios.get(process.env.NEXT_PUBLIC_LCM_FIRSTRUNNER_URL + '/service/exist/mail/' + mail)
        .then(response => {
            res.status(response.status).send(response.data);
        })
        .catch(error => {
            console.log(error)
            //do something with errors
            throw error
        });

    return res.end(); 
}

然后我在 MirageJS 中有一条路由应该可以捕获上面的这个调用process.env.NEXT_PUBLIC_LCM_FIRSTRUNNER_URL + '/service/exist/mail/' + mail,但是它会抛出一个错误:

错误:请求失败,状态码为 404。

是否有解决此电话的方法?

4

0 回答 0