0

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

This is my index.js file:

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.

1 Answer 1

1

Make sure your MongoD instance is running.

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.

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');
});
Sign up to request clarification or add additional context in comments.

2 Comments

mongoose.connect('mongodb://localhost:27017/cats'); can you explain this one?
It's simply telling mongoose to connect to the cats database by connecting to the MongoD instance which is running on port 27017

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.