2016-02-19 71 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数据库 –