javascript - NextJS API |应用程序在部署和开发模式下工作,但在我运行下一次构建时却不行 |被拒绝

标签 javascript reactjs build next.js vercel

描述错误
我已经使用 NextJS 构建了一个具有内部 API 的应用程序,并且我使用 getStaticProps 方法来查询 API。
您可以在此处找到已部署的应用程序:https://tailor-challenge.vercel.app/
还有这里的 github 存储库:enter link description here
该应用程序在开发模式下完美运行,我在 Vercel 中进行的部署也可以正常运行。但是当我运行下一个构建命令时,出现以下错误:

Build error occurred
FetchError: request to http://localhost:3000/api/restaurants failed, reason: connect ECONNREFUSED 127.0.0.1:3000
    at ClientRequest.<anonymous> (/home/silinde87/Jobs/tailor/TailorChallenge/node_modules/node-fetch/lib/index.js:1461:11)
    at ClientRequest.emit (node:events:376:20)
    at Socket.socketErrorListener (node:_http_client:474:9)
    at Socket.emit (node:events:376:20)
    at emitErrorNT (node:internal/streams/destroy:188:8)
    at emitErrorCloseNT (node:internal/streams/destroy:153:3)
    at processTicksAndRejections (node:internal/process/task_queues:80:21) {
  type: 'system',
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED'
}
info  - Collecting page data .npm ERR! code 1
npm ERR! path /home/silinde87/Jobs/tailor/TailorChallenge
npm ERR! command failed
npm ERR! command sh -c next build 
NextJS 内部 API
/api/restaurants/index.js
export default function handler(req, res) {
    const { method } = req;
    
    switch (method) {
        case 'GET':
            getRestaurants();
            break;
        default:
            res.status(405).end(`Method ${req.method} Not Allowed`);
    }

    function getRestaurants() {
        const restaurants = data;
        res.status(200).json(restaurants);
    }
}
/api/restaurants/[id].js
export default function handler(req, res) {
    const { id } = req.query;
    const { method } = req;

    switch (method) {
        case 'GET':
            getRestaurantById();
            break;
        default:
            res.status(405).end(`Method ${req.method} Not Allowed`);
    }

    function getRestaurantById() {
        const restaurants = data.find((el) => el.id.toString() === id.toString());
        if (restaurants) res.status(200).json(restaurants);
        else res.status(405).end(`There is no Restaurant with id: ${id}`);
    }
}
NextJS 的页面
pages/index.js <-- 我在哪里获取餐馆列表
export async function getStaticProps() {
    const res = await fetch(`${process.env.NEXT_PUBLIC_NEXTAUTH_URL}/api/restaurants`);
    const restaurants = await res.json();

    if (!restaurants) {
        return {
            notFound: true,
        };
    }

    return {
        props: {
            restaurants,
        },
    };
}
页面/[id].js
// Return a list of possible value for id
export async function getStaticPaths() {
    const res = await fetch(`${process.env.NEXT_PUBLIC_NEXTAUTH_URL}/api/restaurants`);
    const restaurants = await res.json();

    let paths = restaurants.map(({ id }) => {
        return {
            params: {
                id: id.toString(),
            },
        };
    });
    return { paths, fallback: false };
}

// Fetch necessary data for the restaurant details using params.id
export async function getStaticProps({ params }) {
    const res = await fetch(`${process.env.NEXT_PUBLIC_NEXTAUTH_URL}/api/restaurants/${params.id}`);
    const restaurantData = await res.json();

    return {
        props: {
            restaurantData,
        },
    };
}

最佳答案

我从官方文档中引用了这句话,链接如下:

Note: You should not use fetch() to call an API route in >getStaticProps. Instead, directly import the logic used inside >your API route. You may need to slightly refactor your code for >this approach.

Fetching from an external API is fine!


https://nextjs.org/docs/basic-features/data-fetching
基本上是因为 getStaticProps 或 getStaticPaths 里面的代码永远不会在浏览器中执行,所以你可以安全地直接在那里调用数据源。
此外,由于构建时应用程序未运行,因此发生故障,因此 localhost api 不可用。您可以通过在另一个终端窗口中通过“yarn dev”或“npm run dev”运行应用程序并在另一个终端窗口中构建应用程序来确认这一点。这样它应该构建得很好,但不推荐这样做。我会重构代码,以便在这些 getStaticProps 和 getStaticPaths 函数中不获取此应用程序的 api。
getServerSideProps 也是如此,它也只是在服务器上运行,所以你也不应该在那里获取 api。只有浏览器应该对 api 进行获取,例如,如果您希望用户能够将一些数据发布到应用程序。

关于javascript - NextJS API |应用程序在部署和开发模式下工作,但在我运行下一次构建时却不行 |被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68153761/

相关文章:

javascript - 检索表中选择的行值作为数组并将结果发送到 React 上的另一个函数

javascript - 简单的 ReactJS 表单验证问题

ios - 在 Xcode 9 中实现预处理器?

javascript - 添加年份来计算

javascript - Node 中的请求流在被消费之前会发生什么?

javascript - 获取多个元素的第 N 个父元素

reactjs - typescript react : Conditionally optional props based on the type of another prop

meteor - 尝试构建Docker容器,找不到start.sh

build - 在 CentOS/RHEL 上制作和构建实用程序?

javascript - 未捕获的范围错误 : `options.awareOfUnicodeTokens` must be set to `true` to use `YYYY` In date picker