strapi FetchError: request to http://localhost:1337/api/events failed, reason: connect ECONNREFUSED ::1:1337

strapi FetchError: request to http://localhost:1337/api/events failed, reason: connect ECONNREFUSED ::1:1337

如果我使用浏览器以 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}}}

然而,当我使用 Next.JS 访问相同的 link 时,我得到:

FetchError: request to http://localhost:1337/api/events failed, reason: connect ECONNREFUSED ::1:1337

为什么 strapi 拒绝连接?如何修复?

config/index.js

export const API_URL =
    process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'

pages/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,
        }
    }

-----将代码更新为以下但仍然拒绝连接----

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

pages/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,
    }
}

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

在您的抓取中,您需要从 Strapi 传递不记名授权令牌。您将在 Strapi Admin > Settings > API tokens > Create new API token 中找到您的令牌,现在复制该新令牌并在您的 fetch 调用中使用它。 例如你的令牌是 XYZ123。像这样使用它:

const res = await fetch(`${API_URL}/api/events`, { headers: { Authorization: `bearer XYZ123` } } )
// 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'

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