search

Home  >  Q&A  >  body text

node.js - 在windows服务器配置了mongo数据库并已经开启,为什么mongoose依然无法连接?

在windows服务器配置了mongo数据库并已经开启,为什么mongoose依然无法连接?

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var models = require('./models');
var db = mongoose.connect("mongodb://localhost:27017");
db.connection.on("error",function(err){
    console.log("链接失败"+err)
})
db.connection.on("open",function(){
    console.log("链接成功")
})

链接的时候提示fail to connect to server

前提是mongodb的服务是已经打开的

是因为win32的mongodb版本太低,还是因为其他原因?求指教

怪我咯怪我咯2871 days ago628

reply all(2)I'll reply

  • 阿神

    阿神2017-04-17 15:03:48

    I also use nodejs in the window system, but I use express+mongoose. In order to facilitate opening the server, I slightly modified the code in bin/www:

    var exec = require('child_process').exec;
    exec('mongod --dbpath e:/mongodb/db', {
        cwd: 'e:/mongodb/bin'
    });

    Before starting the server, open a sub-process and let this sub-process start the mongodb service

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:03:48

    Usually it still means that the mongodb server did not start normally. Please first check whether there is a mongod process running and whether port 27017 is listening.

    reply
    0
  • Cancelreply