本文介绍了MongoNetworkError:第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 nodeJS 的新手,通过跟随 youtube 上的预告片开始学习,一切都很顺利,直到我添加了 mongodb 的连接功能,

mongo.connect("mongodb://localhost:27017/mydb")

当我在 cmd (node start-app) 上运行我的代码时,得到以下错误,

MongoNetworkError:第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

有人能解释一下我错过了哪一步吗?我的代码:

var express = require("express");var MongoClient = require('mongodb');var url = "mongodb://localhost:27017/mydb";var webService = require("./webService");var server = express();MongoClient.connect(url, function (err, db) {如果(错误)抛出错误;console.log("数据库已创建!");db.close();});server.use(express.urlencoded({extended: true }));server.set('views', __dirname);server.get('/', function (request, response) {response.sendFile(__dirname + '/MainPage.html');});server.get('/Sign', function (request, response) {response.render(__dirname + '/Sign.ejs');});server.post("/signUp", webService.signUp);server.post("/createUser", webService.createUser);服务器.听(5500);
解决方案

您必须先在系统中安装 MongoDB 数据库服务器并启动它.

使用以下链接安装MongoDB

https://docs.mongodb.com/manual/installation/>

如果你已经安装了 MongoDB 检查服务器是否处于哪个状态(启动/停止).尝试通过 mongo shell 客户端连接.

I'm new in nodeJS, started learning by following a trailer on youtube, everything goes well until I added the connect function if mongodb,

mongo.connect("mongodb://localhost:27017/mydb")

when I run my code on cmd (node start-app), get the following error,

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

Could someone explain me which step I missed ?my code :

var express = require("express");
var MongoClient = require('mongodb');
var url = "mongodb://localhost:27017/mydb";
var webService = require("./webService");
var server = express();

MongoClient.connect(url, function (err, db) {
    if (err) throw err;
    console.log("Database created!");
    db.close();
});

server.use(express.urlencoded({ extended: true }));

server.set('views', __dirname);

server.get('/', function (request, response) {
    response.sendFile(__dirname + '/MainPage.html');
});

server.get('/Sign', function (request, response) {
    response.render(__dirname + '/Sign.ejs');
});

server.post("/signUp", webService.signUp);

server.post("/createUser", webService.createUser);

server.listen(5500);
解决方案

You have to install MongoDB database server first in your system and start it.

Use the below link to install MongoDB

https://docs.mongodb.com/manual/installation/

If you have installed MongoDB check if the server is in which state (start/stop). Try to connect through mongo shell client.

这篇关于MongoNetworkError:第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:02