Andrew  Malyar
Andrew Malyar

Reputation: 11

connect mongoDb to nodejs on localhost

I'm trying to use mongoDB for my app. I have 2 databases and use next code to connect:

var express = require("express");
var router  = express.Router();
var mongojs = require("mongojs");

//var mongo_db = 
//mongojs("mongodb://xxxxx:xxxxx@some_adress/cat_mean_db", ["tasks"]);

var mongo_db = mongojs("mongodb://xxxxx:xxxxx@localhost:3000/cat_db", 
["tasks"]);

//get all docs(pages)
router.get("/tasks", function (req, res, next ) {
mongo_db.tasks.find(function (error, tasks) {
    if(error)
        res.send(error);
    res.json(tasks);
});
});

if I use this db

var mongo_db = 
mongojs("mongodb://xxxxx:xxxxx@some_adress/cat_mean_db", 
["tasks"]);

everything is working well, but when I try to use db on localhost I got an exeption: connection 0 to localhost:3000 closed

the local db is exist and has user for shure.

Upvotes: 1

Views: 2093

Answers (1)

Valentin Duboscq
Valentin Duboscq

Reputation: 978

From mongoose documentation on NPM :

Note: If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed.

You should try using 127.0.0.1 instead of localhost.

Upvotes: 3

Related Questions