Strapi FetchError:请求 http://localhost:1337/api/events 失败,原因:连接 ECONNREFUSED ::1:1337

pre*_*ton 22 node.js strapi next.js

如果我使用浏览器以 PUBLIC(未经身份验证的用户)身份访问 localhost:1337/api/events,我会收到以下返回信息:

{"data":[{"id":1,"attributes":{"name":"Throwback Thursday with DJ Manny Duke","slug":"throwback-thursday-with-dj-manny-duke","venue":"Horizon Club","address":"919 3rd Ave New York, New York(NY), 1002","date":"2022-07-20T02:00:00.000Z","time":"10:00 PM","createdAt":"2022-04-12T02:05:08.246Z","updatedAt":"2022-04-12T02:17:16.760Z","publishedAt":"2022-04-12T02:05:16.192Z","performers":"DJ Manny Duke","description":"Description for the vent of DJ Manny Duke"}},{"id":2,"attributes":{"name":"Boom Dance Festival Experience","slug":"boom-dance-festival-experience","venue":"Blackjacks","address":"123 Lexington","date":"2022-04-25T16:00:00.000Z","time":"8:00 PM","createdAt":"2022-04-12T02:26:32.123Z","updatedAt":"2022-04-12T02:26:33.540Z","publishedAt":"2022-04-12T02:26:33.538Z","performers":"DJ LUKE, DJ BLACKJACK","description":"Whatever Description"}},{"id":3,"attributes":{"name":"Encore Night Boat Party","slug":"encore-night-boat-party","venue":"Encore","address":"12343 New York","date":"2022-11-14T16:00:00.000Z","time":"7:00 PM","createdAt":"2022-04-12T02:28:06.028Z","updatedAt":"2022-04-12T02:28:36.292Z","publishedAt":"2022-04-12T02:28:07.622Z","performers":"BAD BOY BILL","description":"Description of Encore"}}],"meta":{"pagination":{"page":1,"pageSize":25,"pageCount":1,"total":3}}}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用 Next.JS 访问相同的链接时,我得到:

FetchError: request to http://localhost:1337/api/events failed, reason: connect ECONNREFUSED ::1:1337
Run Code Online (Sandbox Code Playgroud)

为什么 Strapi 拒绝连接?怎么修?

配置/index.js

export const API_URL =
    process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'
Run Code Online (Sandbox Code Playgroud)

页面/index.js

...

export async function getStaticProps() {
        const res = await fetch(`${API_URL}/api/events`)
        const events = await res.json()
    
        return {
            props: { events: events.slice(0, 3) },
            revalidate: 1,
        }
    }
Run Code Online (Sandbox Code Playgroud)

-----更新了下面的代码但仍然拒绝连接----

config/index.js 导出 const API_URL = 'http://localhost:1337'

页面/index.js

export async function getStaticProps() {
    const res = await fetch(`${API_URL}/api/events`, {
        headers: {
            Authorization: `bearer thetoken`,
        },
    })
    const events = await res.json()

    return {
        props: { events: events.slice(0, 3) },
        revalidate: 1,
    }
}
Run Code Online (Sandbox Code Playgroud)

----下面是错误的屏幕截图,2 个控制台显示客户端 (NEXT.JS) 和服务器 (STRAPI) 都在运行----

在此输入图像描述

pre*_*ton 48

// export const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://0.0.0.0:1337'
// COMMENTED OUT LINE ABOVE WORKS TOO JUST LIKE THE UNCOMMENTED CODE BELOW
export const API_URL =
    process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:1337'

// BELOW IS NOT WORKING FOR SOME ODD REASON
// export const API_URL = 'http://localhost:1337'
Run Code Online (Sandbox Code Playgroud)

  • 极好的。将“localhost”更改为“127.0.0.1”就可以了。 (12认同)

Gul*_*ati 20

指定 127.0.0.1:1337 而不是 localhost 解决了该问题


小智 10

我遇到了同样的问题,直到我发现问题是我的节点版本高于 v16