做一个练习,我必须构建一个允许注册用户并编辑它们的应用程序。我按照书中的说明操作,但仍然卡住了,因为我收到了错误:
未处理拒绝MongoError:无法连接到服务器 在Collection.listIndexes(/home/mihaly/meanbook/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1732:11) at indexInformation(/home/mihaly/meanbook/node_modules/mongoose/node_modules/mongodb/lib/db.js:1607:25) 在Db.indexInformation(/home/mihaly/meanbook/node_modules/mongoose/node_modules/mongodb/lib/db.js:1571:44) 在ensureIndex(/home/mihaly/meanbook/node_modules/mongoose/node_modules/mongodb/lib/db.js:1066:8) 在Db.ensureIndex(/home/mihaly/meanbook/node_modules/mongoose/node_modules/mongodb/lib/db.js:1042:44) 在ensureIndex(/home/mihaly/meanbook/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1802:13) 在Collection.ensureIndex(/home/mihaly/meanbook/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1790:44) 在tryCatcher(/home/mihaly/meanbook/node_modules/connect-mongo/node_modules/bluebird/js/release/util.js:11:23) at ret [as ensureIndexAsync](eval at(/home/mihaly/meanbook/node_modules/connect-mongo/node_modules/bluebird/js/release/promisify.js:184:12), :13:39) 在MongoStore.setAutoRemoveAsync(/home/mihaly/meanbook/node_modules/connect-mongo/src/index.js:123:40) 在MongoStore.handleNewConnectionAsync(/home/mihaly/meanbook/node_modules/connect-mongo/src/index.js:116:18) 在MongoStore(/home/mihaly/meanbook/node_modules/connect-mongo/src/index.js:95:26) 在对象。 (/home/mihaly/meanbook/CH26/auth_server.js:20:10) 在Module._compile(module.js:410:26) at Object.Module._extensions..js(module.js:417:10) 在Module.load(module.js:344:32) 在Function.Module._load(module.js:301:12) 在Function.Module.runMain(module.js:442:10) 在启动时(node.js:136:18) 在node.js:966:3
这是我的文件的样子......
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
var mongoStore = require('connect-mongo')({session: expressSession});
var mongoose = require('mongoose');
require('./models/user_model.js');
var conn = mongoose.connect('mongodb://localhost/myapp');
var app = express();
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(expressSession({
secret: 'SECRET',
cookie: {maxAge: 60*60*1000},
store: new mongoStore({
db: mongoose.connection.db,
collection: 'sessions'
})
}));
require('./routes')(app);
app.listen(8080);
我想我已经安装了所有必要的模块,并且mongo正在运行。有谁知道我犯了哪个错误?