我是一名初级大学生,用node.js测试mongodb,在我的代码中我试图使用mongoose连接到mongodb,但由于某种原因我的mongoose无法连接数据库,我检查了以下内容:
实际安装mongodb,我通过brew安装mongodb,mongod和mongo工作 mongoose在我的目录下面是我的错误,任何帮助都是apreciated
Express server listening on port 3000
/path/1_mongodb/connect_to_mongo/app.js:17
throw err;
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (path/1_mongodb/connect_to_mongo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:636:74)
at EventEmitter.emit (events.js:106:17)
at null.<anonymous> (/path/1_mongodb/connect_to_mongo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:139:15)
at EventEmitter.emit (events.js:98:17)
at Socket.<anonymous> (/path/1_mongodb/connect_to_mongo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:476:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:426:14
at process._tickCallback (node.js:415:13)
App.js
var express = require('express'),
routes = require('./routes'),
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/todo_development')
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var Task = new Schema({
task: String
});
var Task = mongoose.model('Task', Task);
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
编辑:所以我意识到当尝试使用node.js访问mongodb时,你应该首先运行mongod。