如何使用 next.js 应用路由器获取 api?

问题描述 投票:0回答:1

我在 app/api/create- payment-intent.js 中定义了一个 api 如何使用 fetch 函数访问它?当我尝试

fetch("/api/create-payment-intent"
时,我得到 404。任何人都可以帮助我解决这个问题吗?找不到有关获取内部 api 的文档

next.js
1个回答
0
投票

要在应用程序路由器中处理 API 调用,您应该将 create- payment-intent 转换为文件夹并在其中创建一个 Route.js 文件。

文件路径:

./app/api/create-payment-intent/route.js

在该文件中,您应该编写如下内容:

export const POST = async (req, res) => {
    try {
       // your logic goes here

    } catch (error) {
        return handleApiError(error);
    }
};

此设置应该允许您正确访问您的 API。

© www.soinside.com 2019 - 2024. All rights reserved.