从nodejs到nextjs获取graphql数据(SSR)

v09wglhw  于 2023-01-20  发布在  其他
关注(0)|答案(1)|浏览(188)

我有nextjs项目,在那里我使用SSR从nodejs(graphql)获取数据,但它只工作在本地主机(开发/生产),当我在vercel上上传项目时,我得到“500:内部服务器错误”,我将在下面分享我的代码和所需的详细信息:
我在Nextjs上使用“graphql-request”

import { gql, request } from "graphql-request";

export async function getServerSideProps(context) {

  const endPoint = "http://localhost:4000/";

  const query = gql`
    query {
      exactlyUser(id: "63c3cf0aea56b467893c92d3") {
        username
      }
    }
  `;

  const data = await request(endPoint, query);

  return {
    props: {
      data: data,
    },
  };
}

我也给予你vercel误差:

[HEAD] /_next/data/U_aH1k5dl2EQA1IzXATL6/index.json
2023-01-17T16:52:11.277Z    ddd8e632-1ffa-4772-94c0-aa01e2348bb7    ERROR   FetchError: request to http://localhost:4000/ failed, reason: connect ECONNREFUSED 127.0.0.1:4000
    at ClientRequest.<anonymous> (/var/task/node_modules/node-fetch/lib/index.js:1491:11)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:494:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  page: '/'
}
RequestId: ddd8e632-1ffa-4772-94c0-aa01e2348bb7 Error: Runtime exited with error: exit status 1
Runtime.ExitError
gzszwxb4

gzszwxb41#

当您将Next.js应用程序部署到Vercel时,API似乎无法访问。这是导致“500:内部服务器错误”。
尝试将API端点URL更改为GraphQL API的公共访问URL,而不是使用“http://localhost:4000/"。这应允许Next.js应用在Vercel上运行时从API获取数据。
避免硬编码API端点。

相关问题