Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 错误:连接已关闭-MongoDB节点_Node.js_Mongodb - Fatal编程技术网

Node.js 错误:连接已关闭-MongoDB节点

Node.js 错误:连接已关闭-MongoDB节点,node.js,mongodb,Node.js,Mongodb,好的,我是一名Node/MongoDB新手,目前正在学习本教程: 这是我的密码,苏斯法 var MongoClient = require('mongoDB').MongoClient; MongoClient.connect("mongodb://localhost:8888/exampleDb", function(err, db){ if( !err ){ console.log("We are connected");

好的,我是一名Node/MongoDB新手,目前正在学习本教程:

这是我的密码,苏斯法

    var MongoClient = require('mongoDB').MongoClient;

    MongoClient.connect("mongodb://localhost:8888/exampleDb", function(err, db){
        if( !err ){
             console.log("We are connected");
        } else console.log(err);
    });
但是,当我运行node/node/index.js时,我的控制台日志:

    [Error: connection closed]
我尝试过禁用防火墙和添加自动重新连接:true,但都不起作用。我似乎无法在网上找到问题,有什么问题吗

编辑:这是我设置服务器的方式

    var server = require("./server");
    var router = require("./router");

    // in router.js
    function route(handle, pathname, response){
        console.log("About to route a request for "+pathname);
        if( typeof handle[pathname] === "function"){
            handle[pathname](response);
        } else {
            console.log("404");
            response.writeHead(404, {"Content-Type":"text/plain"});
            response.write("404 not found");
            response.end();
        }
    }

    exports.route = route;

    // in server.js
    var http = require("http");
    var url = require("url");

    function start(route, handle){
        function onRequest(request, response){
            var pathname = url.parse(request.url).pathname;
            route(handle, pathname, response);
        }

        http.createServer(onRequest).listen(8888);
    }
    exports.start = start;

    server.start(router.route, handle);

好的,你的错误很简单。默认情况下,mongoDB在27017端口运行,在代码中,您尝试连接8888端口上的mongod实例。此端口用于您的节点项目,而不是MongoDB

首先,您应该启动mongod实例。如果您在Ubuntu中,可以从以下内容开始:

sudo service mongodb start
如果您在另一个平台上,并且mongodb路径在您的path env中,您只需使用以下命令即可启动它:

mongod
通过以下方式更改连接uri:

var MongoClient = require('mongoDB').MongoClient;

MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db){
    if( !err ){
         console.log("We are connected");
    } else console.log(err);
});

Woops假警报,我还没有启动mongod.exe。因此,对于遇到此错误并搜索web的任何人:确保已安装mongodb,并在安装/bin目录中运行mongod.exe可执行文件

在控制台中尝试
mongo localhost/exampleDb--port 8888
以查看是否可以在mongodb实例中正常连接是否可以使用mongo连接到mongod8888端口上的客户端?我不确定如何运行该代码。在我执行“node/node/index.js”之后,我得到了“console.logs”,但是除非关闭命令提示符,否则无法键入任何内容。如果我在“node/node/index.js”之前键入它,我会得到一个错误,即“mongo”不能被识别为命令。我已经添加了用于设置服务器的代码。您是否已打开mongod
sudo服务mongodb start