我一直在尝试测试我的项目,即使我已经运行并且可以在CMD提示符下连接到它,它也不会连接到mongodb。
这是错误
C:\Users\RockConqueredMe\Projects\volunteer-v.1.0\node_modules\mongoose\node_mod
ules\mongodb\lib\server.js:258
process.nextTick(function() { throw err; })
^
MongoError: failed to connect to server [mongodb:27017] on first connect
at Pool.<anonymous> (C:\Users\RockConqueredMe\Projects\volunteer-v.1.0\node_
modules\mongoose\node_modules\mongodb-core\lib\topologies\server.js:309:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (C:\Users\RockConqueredMe\Projects\volunteer-v.1.0
\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:270:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (C:\Users\RockConqueredMe\Projects\volunteer-v.1.0\nod
e_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:175:49
)
at Socket.g (events.js:291:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at connectErrorNT (net.js:1020:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
以下是app.js
中的代码 var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize()); app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users); //passport config var
Account = require('./models/account');
passport.use(new LocalStrategy(Account.authenticate()));
passport.serializeUser(Account.serializeUser());
passport.deserializeUser(Account.deserializeUser()); //mongoose
mongoose.connect('mongodb:localhost/passport_local_mongoose_express4');
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler app.use(function(err, req, res, next) { // set
locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page res.status(err.status || 500);
res.render('error'); });
module.exports = app;
答案 0 :(得分:2)
您的MongoDB连接网址无效。它应该是:
mongoose.connect('mongodb://localhost/passport_local_mongoose_express4');
请注意//
之后的mongodb:
。