You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NextJS route.ts中使用Axios实例触发GET请求404错误求助

Next.js API路由引入Axios实例触发404错误求助

我在Next.js的API路由中尝试使用实例化的Axios,但只要在route.ts里引入Axios实例,客户端就会出现404路由未找到错误;注释掉相关代码后一切恢复正常。(补充:我已为Axios配置了agent)

使用版本:

  • next "13.4.19"
  • axios "^1.5.0"

axiosInstance.ts 代码

import axios from "axios";

export default axios.create({
    baseURL: 'http://localhost:8080',
    headers: {
        'Content-Type': 'application/json',
    }
});

route.ts 代码

import axiosInstance from "@/lib/axiosInstance";
import { NextResponse } from "next/server";

//*
function axiosGet() {
    axiosInstance.get('/api/v2/tenant').then((response) => {
        return NextResponse.json(response.data, { status: 200 });
    }).catch((error) => {
        return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
    })
}
//*/

export async function GET() {
    return NextResponse.json({ message: 'Hello World' }, { status: 200 });
}

补充截图

  • 文件夹结构:文件夹结构
  • 404错误页面:404错误

内容的提问来源于stack exchange,提问作者Papp Zoltán

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 15:18:55