Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

625
Visualizações
Why can't I connect mongodb using node js?

I have started my mongodb server and the software can actually connect it.
Mongodb started image
Error Gif
My node js code is

mongoose.connect("mongodb://localhost:27017/chat", { useNewUrlParser: true }).catch(err => console.log(err));

and it give me the error

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connection 0 to localhost:27017 closed
    at Socket.<anonymous> (D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\connection\connection.js:439:9)
    at Object.onceWrapper (events.js:422:26)
    at Socket.emit (events.js:315:20)
    at TCP.<anonymous> (net.js:673:12)]
    at Pool.<anonymous> (D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\topologies\server.js:438:11)
    at Pool.emit (events.js:315:20)
    at D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\connection\pool.js:562:14
    at D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\connection\pool.js:995:11
    at callback (D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\connection\connect.js:75:5)
    at D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\connection\connect.js:101:9
    at _callback (D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\connection\connection.js:328:7)
    at Connection.errorHandler (D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\connection\connection.js:343:7)
    at Object.onceWrapper (events.js:422:26)
    at Connection.emit (events.js:315:20)
    at Socket.<anonymous> (D:\Web开发\fake-chatroom\server\node_modules\mongodb\lib\core\connection\connection.js:437:12)
    at Object.onceWrapper (events.js:422:26)
    at Socket.emit (events.js:315:20)
    at TCP.<anonymous> (net.js:673:12)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

From the image you posted, mongodb is listening on the default port, 27017. Your code is attempting to open a connection to port 28019 which is the cause of the error.

Use the default port number in your code:

mongoose.connect("mongodb://localhost:27017/chat", { useNewUrlParser: true }).catch(err => console.log(err));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda