I am following a tutorial found here. However I am getting the following error:
Unable to connect to the mongoDB server. Error: { [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
name: 'MongoError',
message: 'connect ECONNREFUSED 127.0.0.1:27017' }
I have verified using the command found here which returned 1:
ps -ef | grep mongod | grep -v grep | wc -l | tr -d ' '
I have also ran
ps -ef | grep mongod
And received the following:
0 4165 418 0 Fri12pm ttys000 0:00.03 sudo mongod
501 6165 418 0 9:54am ttys000 0:00.00 grep mongod
My javascript file I am trying to run is as follows:
var fs = require('fs');
var http = require("https");
var express = require('express');
var app = express();
var path = require('path');
var http = require("http");
var url = require("url");
var req = require('request')
var pem = require('pem');
var cors = require("cors");
var mongodb = require('mongodb').MongoClient;
var url = 'mongodb://127.0.0.1:27017/Rewards';
// Use connect method to connect to the Server
mongodb.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
// do some work here with the database.
//Close connection
db.close();
}
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static(path.join(__dirname, '../')));
app.listen(process.env.PORT || 8080);
app.options('*', cors());
app.all('/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8080");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
next();
});
app.get('/', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.writeHead(200, {'Content-Type': 'text/plain'});
contents = fs.readFileSync("sliderImages.json", "utf8");
console.log(path.join(__dirname, '/sliderImages.json'));
res.end(contents);
});
Things I have tried:
- Restarting the service
- Changing the port number from 27017 to others
- Taking out code so it is bare bones to verify
- This links information
- This links information
- This links information
I am fairly new to Node JS and Mongo DB so trying to debug this is a bit difficult. I have tried many things from other related posts both on and off StackOverflow but cannot seem to figure out why I am getting this error.
I do have the following installed as I read Mongo DB requires them:
- mongodb-core
- bson
- kerberos
- node-gyp
As well as :
- Mongoose
- Mongo
- Node
I am posting my code; perhaps a better developer who knows these well can explain to me what exactly is going on and why this problem is occurring. I have read from a source I cannot find the link to that this can occur because of a few key issues such as not closing the database connection, no database, Mongo not running and a few others.
Entering Mongod:
2016-05-31T11:18:44.936-0400 I CONTROL [initandlisten] MongoDB starting : pid=10385 port=27017 dbpath=/data/db 64-bit host=RBCs-MacBook- Pro-3.local
2016-05-31T11:18:44.936-0400 I CONTROL [initandlisten] db version v3.2.6
2016-05-31T11:18:44.936-0400 I CONTROL [initandlisten] git version: 05552b562c7a0b3143a729aaa0838e558dc49b25
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2h 3 May 2016
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] allocator: system
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] modules: none
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] build environment:
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] distarch: x86_64
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] target_arch: x86_64
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] options: {}
2016-05-31T11:18:44.937-0400 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2016-05-31T11:18:44.937-0400 W - [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
2016-05-31T11:18:44.937-0400 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2016-05-31T11:18:44.937-0400 I CONTROL [initandlisten] dbexit: rc: 100