mongoose MongoNetworkError:连接0到localhost:27017关闭

hec6srdp  于 2024-01-08  发布在  Go
关注(0)|答案(2)|浏览(153)

我不能让它工作。我已经打开了mongo和mongod,这是我在git Bash或cmd中编写“node server.js”时得到的结果:

  1. Running on server27017
  2. Not connected to database MongoNetworkError: connection 0 to localhost:27017
  3. closed

字符串
这是我的代码

  1. var express = require('express');
  2. var app = express();
  3. var port = 27017;
  4. //Route
  5. app.get('/', function (req, res) {
  6. res.send('Hello world!');
  7. });
  8. const mongoose = require('mongoose');
  9. const option = {
  10. socketTimeoutMS: 30000,
  11. keepAlive: true,
  12. reconnectTries: 30000
  13. };
  14. mongoose.connect('mongodb://localhost:27017/database1', option).then(function(){
  15. //connected successfully
  16. console.log('Successfully connected to database');
  17. }, function(err) {
  18. //err handle
  19. console.log('Not connected to database ' + err);
  20. });
  21. //Listen to port
  22. app.listen(port, function(){
  23. console.log('Running on server' + port);
  24. });


当我在浏览器中写入“http://localhost:27017/database1“时,我得到“看起来你试图在本地驱动程序端口上通过HTTP访问MongoDB”。

vs3odd8k

vs3odd8k1#

Express端口不应与mongodb端口相同

bbuxkriu

bbuxkriu2#

如果您正在连接到mlab,并且此错误是因为您必须添加系统IP地址。
您可以在网络访问选项卡中添加系统的IP地址

相关问题