Node / GraphQL 和 Koa:地址已在使用 :::3000

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

我正在尝试将 GraphQL 实现到我的 KoaJs 服务器。但是,每次 Nodemon 在更改时重新启动时,我都会在控制台中收到错误:

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1334:16)
    at listenInCluster (node:net:1382:12) 
  ...
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...

这是我的代码:

import { ApolloServer, gql } from "apollo-server-koa";
import { ApolloServerPluginDrainHttpServer } from "apollo-server-core";
import Koa from "koa";
import http from "http";

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => "Hello world!",
  },
};

async function startApolloServer() {
  const httpServer = http.createServer();
  const server = new ApolloServer({
    typeDefs,
    resolvers,
    csrfPrevention: true,
    cache: "bounded", 
    plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
  });

  await server.start();
  const app = new Koa();
  server.applyMiddleware({ app, path: "/graphql" });
  httpServer.on("request", app.callback());
  await new Promise<void>((resolve) =>
    httpServer.listen({ port: 3000 }, resolve)
  );
  console.log(`🚀 Server ready at http://localhost:3000${server.graphqlPath}`);
  return { server, app };
}

startApolloServer()

如何修复该错误? 感谢您的帮助!!

graphql graphql-js koa
2个回答
0
投票

startApolloServer()
之前添加以下代码:

process.on('SIGINT', function () {
    console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");
    // some other closing procedures go here
    process.exit(0);
});

复制自https://stackoverflow.com/a/53535316/14791867


-1
投票

我认为您正在尝试在后台运行另一个服务或同一服务正在使用的端口。

你可以尝试:

  1. 您可以找到运行在3000端口的服务并将其关闭。

  2. 您可以更改正在使用的端口。例如,5000。

寻找服务:

Windows:

netstat -ano -p tcp | find "3000"

Linux 或 MacOS

lsof -iTCP -sTCP:LISTEN -n -P | grep :3000

使用 pid 终止服务。然后再次运行您的服务器。

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