我尝试将Mongoose与我的Node.js连接,但它总是显示此错误。我正在关注this教程来创建一个身份验证系统。编写server.js文件并尝试修复routes.js中的问题后,我收到错误,我不知道原因,请帮助。
以下是我的代码和错误:
server.js
// server.js
// set up ======================================================================
// get all the tools we need
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var configDB = require('./config/database.js');
// configuration ===============================================================
mongoose.connect(configDB.url); // connect to our database
// require('./config/passport')(passport); // pass passport for configuration
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser()); // get information from html forms
app.set('view engine', 'ejs'); // set up ejs for templating
// required for passport
app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// routes ======================================================================
require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport
// launch ======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);

routes.js:
module.exports = function(app, passport) {
//...
};

的package.json
{
"name": "node-authentication",
"main": "server.js",
"dependencies" : {
"express" : "~4.0.0",
"ejs" : "~0.8.5",
"mongoose" : "~3.8.1",
"passport" : "~0.1.17",
"passport-local" : "~0.1.6",
"passport-facebook" : "~1.0.2",
"passport-twitter" : "~1.0.2",
"passport-google-oauth" : "~0.1.5",
"connect-flash" : "~0.1.1",
"bcrypt-nodejs" : "latest",
"morgan": "~1.0.0",
"body-parser": "~1.0.0",
"cookie-parser": "~1.0.0",
"method-override": "~1.0.0",
"express-session": "~1.0.0"
}
}

// config/database.js
module.exports = {
'url' : 'your-settings-here' // looks like mongodb://<user>:<pass>@mongo.onmodulus.net:27017/Mikha4ot
};
&#13;
错误
The magic happens on port 8080
events.js:141
throw er; // Unhandled 'error' event
^
Error: failed to connect to [undefined:27017]
at null.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ouestcharlie/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:556:25)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at null.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ouestcharlie/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ouestcharlie/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:996:8)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
&#13;
答案 0 :(得分:0)
Mongoose.connect()需要格式为
的参数 "mongodb://<hostname>/<databasename>"
相反,它已经在您的database.js中配置了以下内容:
"your-settings-here"
我猜这是错的。只需在database.js中填写一个不错的连接URL,例如:&#34; mongodb:// localhost / my-test-db&#34; (请使用正确的值替换),此错误将消失。
答案 1 :(得分:0)
这是连接默认端口(27017)上本地运行的myapp数据库所需的最低要求。如果本地连接失败,请尝试使用127.0.0.1而不是localhost。有时在更改本地主机名时可能会出现问题。 [来源:http://mongoosejs.com/docs/connections.html]
我有同样的错误。我重新安装了mongoDB,但它没有用。
我通过使用以下网址连接到localhost解决了我的问题。
mongoose.connect('mongodb://127.0.0.1:27017/database-name')
;