【问题标题】:error in running a node js file运行节点js文件时出错
【发布时间】:2016-02-20 05:20:40
【问题描述】:

每当我在 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');
});

我正在与一些教程一起学习,但这是一个我不太了解的非常奇怪的错误。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案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');
    });
    

    【讨论】:

    • mongoose.connect('mongodb://localhost:27017/cats');你能解释一下这个吗?
    • 它只是告诉猫鼬通过连接到端口 27017 上运行的 MongoD 实例来连接到猫数据库
    猜你喜欢
    • 2016-11-17
    • 2018-02-02
    • 2020-04-05
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2016-06-29
    • 2018-02-19
    相关资源
    最近更新 更多