我是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);
npm install mongodb
。我们正在谈论实际上为你的平台“安装服务器”。 - Neil Lunn