运行节点js文件时出错

时间:2016-02-19 21:16:23

标签: node.js mongodb mongoose

每当我在webstorm上运行index.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 个答案:

答案 0 :(得分:1)

确保您的MongoD实例正在运行。

如果它没有打开命令提示符并输入mongod来启动它。我假设您已经添加了MongoDB安装目录的路径。在你的PATH ENVIRONMENT VARIABLE。

同时将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');
});