简体   繁体   中英

Getting Mongo to Connect to Express App

Yosemite Mongo user working through a MEAN book. I've been following the steps exactly, but the MongoDB isn't connecting as expected. I installed Mongo once before to mess around with importing data, but didn't get too far. Below is all the Mongo related code I have besides package.json:

In app.js I have require('./app_server/models/db'); and in that folder I have:

var mongoose = require( 'mongoose' );
var dbURI = 'mongodb://localhost/Loc8r';
mongoose.connect(dbURI);

// CONNECTION EVENTS
mongoose.connection.on('connected', function () {
  console.log('Mongoose connected to ' + dbURI);
});
mongoose.connection.on('error',function (err) {
  console.log('Mongoose connection error: ' + err);
});
mongoose.connection.on('disconnected', function () {
  console.log('Mongoose disconnected');
});

I am expecting to get

Express server listening on port 3000
Mongoose connected to mongodb://localhost/Loc8r

But instead get:

Mongoose disconnected
Mongoose connection error: Error: failed to connect to [localhost:27017]

I've tried changing a line to var dbURI = 'mongodb://localhost:3000/Loc8r'; because that's what my computer is using, but then get this error:

Mongoose disconnected
Mongoose connection error: Error: connection closed

Any help is much appreciated thanks.

Mongo was missing the folders it needed MongoDB not working. "ERROR: dbpath (/data/db) does not exist."

Then the server had to be run with "mongod"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM