VPS not respond to request MongoDB-NodeJS - node.js

I bought a VPS server and installed Nodejs and Mongodb in it. Then I transferred my existing database file to mongodb, which I set up on VPS. I made the connection via NodeJS. The database seems to be working, but when I make a request, for example http://serverip:port/fetchData, I get the following error
'It looks like you are trying to access MongoDB over HTTP on the native driver port.'
could you please help me to solve this problem?
Here is NodeJs connection code
const URL = "mongodb://serverIp:port/dbname";
const connection = mongoose
.connect(URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
family:4
})
.then(() => console.log("Successfully Connected DB"))
.catch((e) => console.log("DB CONNECTION ERROR: "+e));
module.export = connection;
Note --> I added my desktop ip to access mongodb database and I can easily manage with my own desktop

HTTP interface for MongoDB Deprecated since version 3.2 :)
Check Mongo Docs: HTTP Status Interface

Related

How do i resolve this Mongo Server Selection Error

Am quiet new to backend and database. I have a solution currently using mongodb. It was working fine till yesterday when i starting having the connect ETIMEDOUT 13.37.254.237:27017 error. Nothing was changed in the URI path or tampered with. It just started and i have not been able to sort it out.
is there any help available please?
I have created another cluster and its working well. But my initial cluster that has datas which are live from clients is not connecting still.
My connection code
I have used these connections code but it has not worked. It was connecting fine all through yesterday but today without tampering with the code, couldn't connect to my mongodb
mongoose.connect(process.env.MONGO_URI,{ useNewUrlParser: true, useUnifiedTopology: true });
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URL);
console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.log(error);
process.exit(1);
}
};
my mongoose connection and the timedout error
Whenever you connect to Mongodb using IPs that keep updating from your system causes this kind of issues.
also this can be due to your network connection. So i will advice you to:
To allow connection from any IP address(but must ensure your URI is not made known to the public to avoid attack/ access from unwanted users.)
2.Check your network status(data)
3. Run the mongo URI on your atlas

Connect to MongoDB using mongoose with proxy

Goal : Connect to a public facing mongodb instance in my local development environment, which is connected to my corporate network.
Background : There is a proxy available for bypassing this corporate firewall. I am developing a Next JS application with mongo as the backend.
Ask : How do I use this proxy to connect to the mongo instance in my node js application?
const db = await mongoose
.connect(process.env.NEXT_PUBLIC_MONGO_URI, {
dbName: "cusum",
useCreateIndex: true,
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.catch((err) => console.log(err.reason));
This is how I connect to my mongo db instance using mongoose, in my node js application. It works when not connected to my corporate network, but times out when connected to my corporate network.
I was able to connect to the instance using Studio 3T with its built in proxy support, however this is just a GUI client for mongo db.
Attempts
I have attempted to use the global-tunnel package to route all http connections to the proxy but it didn't work.
globalTunnel.initialize({
host: "proxy-xx.xxx.com",
port: 8080,
protocol: "http:",
connect: "both",
});
I hope this link can help on a corporate proxy issue
Connect to MongoDB database using mongoose behind a proxy
Also, you can look into Nginx as a proxy, depending on your company configuration, though I see you're working wit Nextjs, most of it will depend on the configuration used at your company

Cannot do raw queries on admin API doesn't work on MongoDB Atlas

My api server wrote with Node.js , database is MongoDB.
API works fine in my local.
When I push the same data to MongoDB Atlas and push my api server to Heroku
MongoDB version 4.2
Call the API I get the error
{"error":"cannot do raw queries on admin in atlas"}
API:
https://movie-with-atlas.herokuapp.com/api/getTwRanking
My API server code about connect to MongoDB Atlas:
const mongoose = require('mongoose');
if (process.env.NODE_ENV !== 'test') {
// MongoDB Atlas
mongoose.connect('mongodb+srv://myAccount:myPassword#cluster0.vcwmp.mongodb.net/movietogo?retryWrites=true&w=majority' , { useNewUrlParser: true });
// if connect with my local API is fine
// mongoose.connect('mongodb://localhost/movietogo');
}
I have checked my Network Access on MongoDB Atlas, set 0.0.0.0/0 (includes your current IP address) for Heroku, the issue still exists.
How do I fix my connect with MongoDB Atlas issue ? Thanks.
It comes from my API code:
I search the collection TwRanking and find the object key theater is TwRanking
getTwRanking (req, res, next) {
console.log('check getTwRanking');
const cityCollection = mongoose.model("TwRanking", MovieSchema, "TwRanking");
cityCollection.find({ "theater": "TwRanking" })
.then(movie => res.send(movie))
.catch(next);
},
I guess it catchs the error.
If look the answer MongoError: cannot do queries on admin in atlas
connect code change like this
mongoose.connect('mongodb://myAccount:myPassword#cluster0-shard-00-00.vcwmp.mongodb.net:27017,cluster0-shard-00-01.vcwmp.mongodb.net:27017,cluster0-shard-00-02.vcwmp.mongodb.net:27017/movietogo?ssl=true&replicaSet=cluster0-shard-0&authSource=admin');
I will get the error:
GET https://movie-with-atlas.herokuapp.com/api/getTwRanking 503 (Service Unavailable)

Can't connect to MongoDB Atlas from CentOS using NodeJS

I've created a cluster on MongoDB Atlas and have successfully connected to it using Compass and also using Node running locally.
When I try to run the same code on my CentOS machine i get an ECONNREFUSEDerror. Here is my Node code
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://myuser:mypass#tablematic-0-z4nuk.gcp.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
console.log("ERROR: " + err);
client.close();
});
myuser and mypass are replaced by my actual username and password here.
I've opened up server's firewall for ports 27015, 27016 and 27017.
Atlas is set up to accept connections from any incoming IP.
I really can't think of what might be causing it...
I've figured it out. It ended up being the cPanel Firewall, even though I've opened the ports through firewall-cmd, my cPanel was still bypassing that configuration with its own firewall.
I'm not 100% sure but my connection strings are usually like
mongodb://user:pass#host.app:27017/db_name

MongoDB connection error getaddrinfo ENOTFOUND Cluster0_shard_name

I'm having an issue with trying to connect to MongoDB. I'm using mongoose here,
console.log("trying here"); // prints
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
});
console.log("Mongo DB connected"); // never reached
And it throws an error
getaddrinfo ENOTFOUND cluster0-shard-00-01-abcde.mongodb.net
I have never had this error before, it used to work just fine -- not sure where to go from here. I've tried building a new cluster and network access by IP is set to public 0.0.0.0. It just keeps throwing the same error.
Note that if you specify useNewUrlParser: true, you must specify a port in your connection string,
like mongodb://localhost:27017/dbname.
The new url parser does not support connection strings that do not have a port, like mongodb://localhost/dbname.
see more info here
one more option, if you use node.js +mongo Atlas:
log in https://cloud.mongodb.com/
go to your cluster0 or
other cluster,
click connect... choose connet your application
choose node.js and then 2.2.12 or later -
you will get connection string that start with mongodb://
insted of mongodb+srv:// (for node js 3.0 or later)
I was facing same error.
How are you using your mongo connection string? and what is your mongoose version?
if your using a latest version of mongoose try using like below string
I changed my string from
mongodb://<user>:<password>...
to
mongodb+srv://<user>:<password>...
check your CONNECTION :
Quick fix is checking your connection, this error happens while your connection is not stable and it've tried to connect more than 30 sc without any success.
//checks connection status
mongoose.connection.on("connected", () => console.log("mongoDb connected!"));
mongoose.connection.on("disconnected", () => console.log("mongoDb disconnected!"));

Resources