Tuesday, 30 May 2017

MongoError on connecting to server with node js with a mlab database

Since yesterday, i've tried so hard to fix this problem but without any result at all...

The problem is not as complicated as it seemed to be. But i'm kinda new to node.js, mongodb.

I tried to connect to a server using mlab.com ( database using mongo). My database is created and i also add users on it so i can connect into.

Then, i created my files for a rest api such server.js, api.js, and status.js

Here is a sample of my server.js where the error is :

// dependencies :

var express = require ('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

// connect to mongoDB :

console.log("Ceci est la version" +mongoose.version);

//mongoose.connect('mongodb://yourdbus:yourdbpw@ds151631.mlab.com:51631/mongodb beachcamp');

//express
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

// routes:



app.use('/api', require('./routes/api'));

//start server :

app.listen(1000);
console.log('Server is running on port 1000');

The sentence within bold creates the problem. When i quote it (using //) the message on my cmd is good : Server is running on port 1000.

But no database connection is created and can't see the localhost:1000/api/status nor using postman on it.

The message error is :

MongoError: failed to connect to server [ds151631.mlab.com:51631] on first connect [MongoError: conn
ect ETIMEDOUT 54.216.50.139:51631]
    at Pool.<anonymous> (C:\Users\34011-14-04\Desktop\myrestapi\node_modules\mongodb-core\lib\topolo
gies\server.js:328:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:191:7)
    at Connection.<anonymous> (C:\Users\34011-14-04\Desktop\myrestapi\node_modules\mongodb-core\lib\
connection\pool.js:280:12)
    at Object.onceWrapper (events.js:293:19)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:194:7)
    at Socket.<anonymous> (C:\Users\34011-14-04\Desktop\myrestapi\node_modules\mongodb-core\lib\conn
ection\connection.js:177:49)
    at Object.onceWrapper (events.js:293:19)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at emitErrorNT (net.js:1279:8)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

Thank you for your tips.

P.S : my goal is to use node.js, mongodb and rest on Android.



via Karro

No comments:

Post a Comment