2016-02-19 56 views
0

每當我在webstorm運行我index.js文件,我得到以下錯誤:錯誤在運行的節點js文件

process.nextTick(function() { throw err; }) 
            ^
Error: connect ECONNREFUSED 127.0.0.1:27017 
    at Object.exports._errnoException (util.js:870:11) 
    at exports._exceptionWithHostPort (util.js:893:20) 
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14) 

Process finished with exit code 1 

這是我index.js文件:

var express = require('express'); 
var app = express(); 

var bodyParser = require('body-parser'); 

var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost/cats'); 

app.use (bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: true 
})); 

var cats = require('./cat_routes.js')(app); 

var server = app.listen(3000, function(){ 
    console.log('running at 3000'); 
}); 

我通過學習邊一邊有一些教程,但這是一個非常奇怪的錯誤,我不明白。

回答

1

確保您的MongoD實例正在運行。

如果沒有打開命令提示符並鍵入mongod來啓動它。我假設你已經添加了你的MongoDB安裝目錄的路徑。在你的PATH環境變量中。

而且改變你的index.js文件到這一點:

var express = require('express'); 
var app = express(); 
var bodyParser = require('body-parser'); 
var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost:27017/cats'); 
app.use (bodyParser.json()); 

app.use(bodyParser.urlencoded({ 
    extended: true 
})); 

var cats = require('./cat_routes.js')(app); 

var server = app.listen(3000, function(){ 
    console.log('running at 3000'); 
}); 
+0

'mongoose.connect( '的MongoDB://本地主機:27017 /貓');'你能解釋一下這個嗎? –

+0

它只是告訴mongoose通過連接到端口27017上運行的MongoD實例連接到cats數據庫 –