简体   繁体   中英

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.

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 .

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