gpt4 book ai didi

node.js - MongoNetworkError : failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127. 0.0.1:27017]

转载 作者:IT老高 更新时间:2023-10-28 13:04:59 30 4
gpt4 key购买 nike

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

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

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

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [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) {
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);

最佳答案

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

使用以下链接安装MongoDB

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

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

关于node.js - MongoNetworkError : failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127. 0.0.1:27017],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50173080/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com