Reputation: 21
I am writing a MEAN application for work and I have come up to a roadblock. My index.js file, which contains my mongo db connection information, seems to be wrong and I am scratching my head to find the solution. Unfortunately all similar issues I see are on the 'nix side. I am not very well versed in Windows so if this is a stupid question, I apologize.
Current index.js file:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var _ = require('lodash');
var dbURI = 'mongodb:127.0.0.1\d$\db\pclistapp';
// Create the app
var app = express();
// Add middleware necessayr for the REST API
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method_Override'));
// Connect to MongoDB
mongoose.connect(dbURI);
//var db = mongoose.connection;
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + dbURI);
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
// Test connection
mongoose.connection.once('open', function() {
console.log('Listening on port 3000... ');
app.listen(3000);
})
Output when I try and run node index.js is MonogError: getaddrinfo ENOTFOUND mongodb mongodb:127. The DB is currently running on port 27017.
I have tried
mongodb:\\"PCname"\d$\db\pclistapp
mongodb:\\localhost\d$\db\pclistapp
mongodb:"PCname"\d$\db\pclistapp
mongodb:localhost\d$\db\pclistapp
and none of them seem to work. I know the UNC works for \"PCname"\d$\db\pclistapp so I am not sure what the issue is.
Upvotes: 0
Views: 198
Reputation: 21
Took a long time but the mistake was simple. I re-wrote the code to condense the errors and it's not a UNC which I need to connect to but via HTTP. Fix that and I was off to the races.
Silly me.
Code:
var mongoURI = "mongodb://localhost:27017/pclistapp";
var MongoDB = mongoose.connect(mongoURI).connection;
MongoDB.on('error', function(err) { console.log(err.message); });
MongoDB.once('open', function() {
console.log("mongodb connection open");
});
Upvotes: 1