NodeJS:MongoDB未在节点应用

时间:2016-04-03 13:46:53

标签: node.js mongodb mongoose

nodeJS中的我的应用程序在运行时拒绝连接到数据库,我使用mongoose ODM连接到数据库。当我运行应用程序时,我收到以下错误。

Server running at  port 8000

/Users/user/www/document-manager/node_modules/mongodb/lib/server.js:242
        process.nextTick(function() { throw err; })
                                      ^
Error: connect ECONNREFUSED 127.0.0.1:27017
    at Object.exports._errnoException (util.js:890:11)
    at exports._exceptionWithHostPort (util.js:913:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1065:14)

以下是我运行的代码:

var express = require('express'),
mongoose = require('mongoose'),
dotenv = require('dotenv'),
bodyParser = require('body-parser'),
router = require("./server/router");

//Initialize App
var app = express();
app.use(bodyParser.json());

//Initialize database connection
mongoose.connect("mongodb://localhost/dms");

router(app);

//Load Dotenv
dotenv.load();

//Set Port
var port = process.env.PORT || 8080;

//Run applicatino
app.listen(port, function(){
    console.log("Server running at  port " + port);
});

如果我单独从终端运行mongod,应用程序将运行。有没有办法一起运行我的数据库服务器和我的应用程序服务器?

0 个答案:

没有答案