一尘不染

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

node.js

我是nodeJS的新手,通过关注youtube上的预告片开始学习,直到我添加了connect函数(如果mongodb,

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

当我在cmd(节点启动应用)上运行代码时,出现以下错误,

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);

阅读 1363

收藏
2020-07-07

共1个答案

一尘不染

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

使用以下链接进行安装 MongoDB

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

2020-07-07