nodejs and mongodb remote access through application - node.js

I have a web app setup using nodejs and mongodb and backbonejs.
When I run my app remotely and try to redirect to a different route through the uri, for example: http://www.myapp.com/route I get a net error.
In my route.js file I have the following:
var mongodb = require('mongodb');
var db = new mongodb.Db('dbname', new mongodb.Server('xx.xx.xxx.xxx', 27017, {}));
where xx.xx.xxx.xxx is my public ip. However, when I change this to anything other than "localhost" and try to run node server.js in the root of my app I get the following error:
Listening on port 3000...
/var/www/html/pages/node_modules/mongodb/lib/server.js:236
process.nextTick(function() { throw err; })
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1012:19)
I have already uncommented bindIp in my /etc/mongod.conf file as suggested in other posts. I did this only to test and just to get it to work, but I plan to go back and play with the ip tables if I get this to work.
How can I access my app remotely?

Related

Authentication problem with connecting to mongodb Atlas

What I want to accomplish is probably the simplest thing ever. I just want to connect to mongodb atlas free tier M0 in nodejs using mongodb driver. I already have created a free account and cluster.
Here is my simple code:
const mongoclient = require('mongodb').MongoClient;
const URI = 'mongodb+srv://mohammed:mypassword#lebran-0qf7m.gcp.mongodb.net/test?retryWrites=true';
mongoclient.connect(URI, { useNewUrlParser: true }, (err, client) => {
if(err) throw err;
console.log('mongodb connected');
db = client.db('mern');
})
Here is what happens when I run the file:
Mohammeds-MacBook-Pro:mern ahmed$ node server.js
/Users/ahmed/Projects/mern/server.js:13
if(err) throw err;
^
Error: queryTxt ESERVFAIL lebran-0qf7m.gcp.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:199:19)
I also tried mongo shell with the following command:
mongo "mongodb+srv://lebran-0qf7m.gcp.mongodb.net/mern" --username mohammed
here is the output I get:
MongoDB shell version v4.0.6
Enter password:
connecting to: mongodb://lebran-shard-00-00-0qf7m.gcp.mongodb.net.:27017,lebran-shard-00-01-0qf7m.gcp.mongodb.net.:27017,lebran-shard-00-02-0qf7m.gcp.mongodb.net.:27017/mern?gssapiServiceName=mongodb&ssl=true
2019-02-20T22:48:57.232+0300 E QUERY [js] Error: Authentication failed. :
connect#src/mongo/shell/mongo.js:343:13
#(connect):1:6
exception: connect failed
I am sure I enter my password right. I have changed several ones and none of them work. I have my IP listed as white list. I also have an admin account. All the prerequisites I can think of.
I am able to connect to the local server but not to the Atlas. This really beginning to drive me crazy.
Any help is greatly appreciated...
**UPDATE:
I just deployed my app to heroku and you know what? It connects!! I could not get it to connect using my computer though. So probably it has to do something with my location or my connection, probably the location.

Node postgres ECONNREFUSED in localhost

This issue is totally driving me insane. I spent months with this, trying to make a SIMPLE NODE APP WORK. I finally managed to make an APP work in a nice server (Heroku) and with mysql. Problem? The server only accepts postgres. And this is my nightmare. I just cannot make it work. Searched dozens of webs and problems, all of them with the same error log as me... but I just cannot figure what to do. I'm totally idiot at configuring things, I cannot even start programming my app.
Error: connect ECONNREFUSED 127.0.0.1:5432
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afteeConnect [as oncomplete] (net.js:1198:14)
My start of server.js
const pg = require('pg');
const connectionString = process.env.DATABASE_URL || 'postgres://myrole:12345#localhost:5432/mydb';
And here the error.
var pool = new pg.Pool();
pool.connect().then(client => {
It crashes right at connection.
I did everything I searched for. I created "myrole" login role with all permission, password "12345", to connect to "mydb" database. I opened "pgAdmin4" application. Connected to "PostgreSQL 10" and "mydb". I saw that the first one connects to port 3000. I tried port 3000 in the connection string. I searched for the service at Windows. It's running. I JUST DID EVERYTHING and nothing works... I installed and made MySQL database to run in local in just 2 hours. But Heroku doesn't accept MySQL and I don't want to put any credit card. What's happening here?
I was having the same issue and I'll put my situation here and hopefully help someone else.
I was testing some AWS Lambda functions and since the code runs in a container and the container has its own localhost so my postgres connection was failing because there is no postgres server running on the container. Remember that your machine's localhost is not the same as the container's one, if your app is running inside a container the instead of localhost use your machine IP.
in your case, you shuld include connectionString inside new Pool() as property of Object
var pool = new pg.Pool({connectionString}); pool.connect().then()
or here is a more detailed version
let c = {
host: 'localhost',
port: 5432,
user: 'user',
password: 'password',
database: 'mydb',
options: `application_name=${app}&application_cmd=${cmd}`
};
const connection_string = {connectionString : `postgresql://${c.user}:${c.password}#${c.host}:${c.port}/${c.database}?${c.options}`};
const pool = new Pool(connection_string);
more details can be found here node-postgres.com

Node.js Net.Socket() to connect to net.tcp web service

So I have a web service running on port 7001 over TCP on:
net.tcp://www.myurl.com:7001/my/webservice
and I want to connect to it using net.Socket:
client.connect(7001, 'myurl.mine.com/my/webservice', function () {
console.log('Connected');
client.write(msg);
});
When i do the above, it gives an exception:
Error: getaddrinfo ENOTFOUND <<my url>>
at GetAddrInfoReqWrap.onLookup [as oncomplete]
when i try connect to it without the /my/webservice, it connects fine and doesnt give an error at the client.connect() stage, but obviously cant find that endpoint so it gives another error when i try to do the client.write()
Any one any idea how to use net.Socket against a web service with a url that isnt just myurl.com:7001 but actually contains a route like myurl.com:7001/my/webservice?
According to node.js docs the version of the method you are using expects only a hostname for a second argument. You are providing hostname + path
In your case I would recommend using a more generalized version of net.connect which accepts an object with parameters. So your code will look something like this:
client.connect({
host: 'myurl.mine.com',
port: 7001,
path: '/my/webservice'
},
function () {
console.log('Connected');
client.write(msg);
}
);

node.js in a dockerfile cant connect to mongolabs via mongoose: getaddrinfo ESRCH

I have done some googling but haven't found anything..
My setup is like this:
OS X, boot2docker, everything local except mongolabs database.
The project is working fine when it's not inside docker.
I am trying to run my nodejs project inside a docker image. I have done this before, but not with a database connection requirement. The problem is that my node app crashes on startup with the information:
/src/node_modules/mongoose/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
^
Error: getaddrinfo ESRCH
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
Does anyone have any idea how to solve this? Is there a DNS flag inside docker that can be set or is anything blocking my connection to the outside?
EDIT:
I connect to my mongodb on mongolabs through mongoose.
var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
and my passport.js file looks like this:
module.exports = {
'url' : 'mongodb://<user>:<password>#ds045027.mongolab.com:45027/database
};

Openshift Zombiejs

I have a Node.js/Express application which I'm trying to deploy to Openshift.
The server itself starts up properly and serves almost everything.
One request to the server opens up a Zombiejs browser, which then crawls some webpages.
When the browser tries to visit a webpage however, I get this error:
{ [Error: bind EACCES] code: 'EACCES', errno: 'EACCES', syscall: 'bind' }
Possibly unhandled Error: bind EACCES
at errnoException (net.js:901:11)
at connect (net.js:747:21)
at net.js:842:9
at asyncCallback (dns.js:68:16)
at Object.onanswer [as oncomplete] (dns.js:121:9)
The browser fails as soon as I call visit:
browser.visit(menus_url).then(function () {
// do things
});
Before doing the above, I start an Express-based server as follows:
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP;
app.listen(server_port, server_ip_address, function() {
console.log('Server running on port ' + server_port);
});
The server starts up fine, however as soon as I make a request that calls browser.visit() the error is raised.
Update 11/15
I still haven't found a solution to this issue.
When the browser is created, it tries to use the ip address of 0.0.0.0 to bind to (https://github.com/assaf/zombie/blob/master/src/zombie/browser.coffee#L1224) by default, which is not available on OpenShift for your use, you need it to bind to your OPENSHIFT_NODEJS_IP ip address, just like you use for your express server. It looks like, at the top of the file that I linked to (https://github.com/assaf/zombie/blob/master/src/zombie/browser.coffee#L42), that one of the Browser options you can pass in is 'localAddress' when creating the Browser object, which should be the OPENSHIFT_NODEJS_IP. I think that will get it fixed up for you.

Resources