运行节点js文件时出错 [英] error in running a node js file

查看:56
本文介绍了运行节点js文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在Webstorm上运行index.js文件时,都会出现以下错误:

Whenever I run my index.js file on webstorm, I get the following error:

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

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

I am learning side by side with some tutorials, but this is a very strange error that I don't really understand.

推荐答案

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

Make sure your MongoD instance is running.

如果未打开,请在命令提示符下键入mongod以启动它.我假设您已将路径添加到MongoDB安装目录.在您的PATH环境变量中.

If it's not open command prompt and type in mongod to start it. I assume that you have added path to your MongoDB installation dir. in your PATH ENVIRONMENT VARIABLE.

还将您的index.js文件更改为此:

Also change your index.js file to this :

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

这篇关于运行节点js文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆