Create mongoose connection behind the university proxy? - node.js

I am building A MERN stack app on linux. The internet only work when we use proxy server of college. I am using mongoose for creating connection with MongoDB Atlas. The issue is that mongoose is unable to connect to MongoDB although i already set the proxy. How can I create connection in mongoose through university proxy server with MongoDB Atlas.
Currently mongoose connection is setup like this:
const CONNECTION_URL =process.env.CLUSTER_URL;
const PORT = process.env.PORT || 3000;
mongoose
.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() =>
app.listen(PORT, () => {
console.log(`server running at PORT ${PORT}`);
}),
)
.catch(error => console.log(error));
Currently university is using HTTP_PROXY=http://172.16.100.7:3022.

Related

I can't connect my express rest api to mongo db atlas, using mongoose

Error: UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
Things I've tried:
Whitelisting all IP addresses under security, inside of my mongo db atlas, by setting a catchall IP address: 0.0.0.0/0
Whitelisting my specific IP address
Changing my password, and making is super insecure and adding it directly to the uri string, to make sure that it's not a credentials issue.
Commenting all other lines of code to test the uri string only.
Using the default port number 3000
My Index.js file
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
// CORE MIDDLEWARE
app.use(express.static('public'));
// CUSTOM MIDDLEWARE
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// MONGO/MONGOOSE
mongoose.connect
(`mongodb+srv://myUsername:MyPassword#ClusterName.id9lz.mongodb.net/database-
name?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const port = 5000 || process.env.PORT;
app.listen(port, () => {
console.log(`Listening to port ${port}...`)
})
NOTE: I've cut out all excess code like dependencies, etc, out of this question, so it's easier to parse through and find an answer faster. I'm completely out of ideas. I don't know what else to even attempt. Thank you in advance
This has happened to me before, i don't know why really, but, after 2 hours, it started to work correctly again, the only thing that comes to my mind is that my internet was kinda weak... But i really don't know why was that happening anyways.
But, you should try to change the way you want to connect your server to mongoDB, i used to do it like that, but after a while, i've had some problems.
So, try to change it like this
const port = 5000 || process.env.PORT;
mongoose.connect
(`mongodb+srv://myUsername:MyPassword#ClusterName.id9lz.mongodb.net/database-
name?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
app.listen(port, () => {
console.log(`Listening to port ${port}...`)
})
console.log('MongoDB connected correctly')
})
And also, make sure you're credentials are correct while trying to connect to MongoDB
Hope i helped you a litle bit !

Can't connect to MongoDB through heroku but works on localhost

Trying to push my simple notes app to heroku but when launched on heroku mongoDB gives me this error
Could not connect to any servers in your MongoDB Atlas cluster.
My backend works perfectly fine while my app is running locally and I have whitelisted all IPs in mongoDB and added MONGODB_URI as a local var in heroku so I'm unsure what is wrong at this point.
Here is my connection.
const mongoose = require('mongoose')
const url = process.env.MONGODB_URI
console.log('Connecting to..', url)
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
.then(result => {
console.log('Connected to MongoDB')
})
.catch((error) => {
console.log(`Error connecting to MongoDB: `, error.message)
})
Fixed the problem by deleting my whitelist in mongoDB Atlas and then adding it back, my server connected on heroku after that.

Express server don't start when I use "mongoose.connect'

I'm doing an App with React, Express and MongoDB,
Everything is OK when I start the Express server with this line :
app.listen(port, () => console.log(`Server running on ${port}`));
But when I try to start the server with this :
mongoose
.connect(
`mongodb+srv://${process.env.USER}:${process.env.PASSWORD}#ofilms-demo-f9iwz.mongodb.net/${process.env.DB}`,
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then(() =>
console.log(
`Server running on ${port} and connection to MongoDB database is OK !`
)
)
.catch((err) => console.log(err));
I can't access to localhost:5000 (server port) and I have no error messages !
The env variables are OK, I logged them..
You're connecting to MongoDB but not actually starting the server (which are two separate things):
mongoose
.connect(
`mongodb+srv://${process.env.USER}:${process.env.PASSWORD}#ofilms-demo-f9iwz.mongodb.net/${process.env.DB}`,
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then(() =>
console.log(`Connection to MongoDB database is OK !`)
// Start the server here
app.listen(port, () => console.log(`Server running on ${port}`));
)
.catch((err) => console.log(err));

MongoNetworkError: failed to connect to server on first connect [MongoError: Authentication failed.]

I'm trying to connect to my MongoDB Atlas database, but it's not working, and I can't seem to figure out why.
I've made sure I don't use illegal characters in my username and password, I've tried different connection strings. I've tried turning off my firewall.
server.js
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const app = express();
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(bodyParser.json());
const db = require("./keys").mongoURI;
mongoose.connect( db, { useNewUrlParser: true } )
.then(() => console.log("MongoDB successfully connected"))
.catch(err => console.log(err));
const port = process.env.PORT || 5000; // process.env.port is Heroku's port if you choose to deploy the app there
app.listen(port, () => console.log(`Server up and running on port ${port} !`));
keys.js
module.exports = {
mongoURI: "mongodb+srv://username:password#mernauth-lksvn.mongodb.net/test?retryWrites=true&w=majority"
};
I replaced the username and password in the connection string with the real username and password.
EDIT: SOLUTION
Even though I used both my IP address and the default IP address MongoDB Atlas suggested for me, neither worked. I guess I didn't have a valid IP address, so once I opened the database to all IP addresses, it worked!

Node/express application failing to connect to Mongodb Atlas using mongoose

I have a node/express application that I am trying to connect to Mongodb Atlas using mongoose.
All of my code is identical to a previous app that I had connect to Atlas (which worked fine). When I run it on my work machine (Windows 10) everything works as expected. However, when I run it on my MacBook Pro (Mojave), the express app runs but the mongoose connection to Atlas throws the following error:
{ Error: queryTxt EBADNAME development-zv5hp.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:196:19)
errno: 'EBADNAME',
code: 'EBADNAME',
syscall: 'queryTxt',
hostname: 'development-zv5hp.mongodb.net' }
server.js
const express = require('express');
const mongoose = require('mongoose');
const app = express();
mongoose
.connect(
'mongodb+srv://client:<PASSWORD>#development-zv5hp.mongodb.net/shop',
{ useNewUrlParser: true }
)
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
What might be causing this issue?
I have checked the Atlas user and password and have whitelisted my IP (in fact whitelisted all IPs)
Using:
node v10.15.3
express v4.16.4
mongoose v5.5.1
Using Google's DNS server 8.8.8.8 and 8.8.4.4 can resolve this issue
please add autoIndex: false its working for me
mongoose
.connect(
'mongodb+srv://client:<PASSWORD>#development-zv5hp.mongodb.net/shop',
{autoIndex: false, useNewUrlParser: true }
This error is caused because the URI 'mongodb+srv://client:<PASSWORD>#development-zv5hp.mongodb.net/shop' that was passed to connect was unable to be resolved. Your DNS server does not know it and thus cannot resolve an IP. Hence ebadname.
change this like Addison mentioned

Resources