var employeeProvider = new EmployeeProvider('localhost', 27017);
What is this 27017?
This code is written in Node.js.
That's most likely a port number. In this case, it's probably specifying the connection info for a MongoDB server (since 27017 is the default port that Mongo uses).
In itself, the code is just invoking the EmployeeProvider function with two arguments.. One string 'localhost', and one number 27017. The code can't mean anything else if you don't know what is the code behind that function. That said, it looks like the function opens a MongoDB connection on the default port (27017) to the mongodb server on the local machine.
Related
so this is my code, I watched a lot of videos how to connect mongo db to node and I tried every possible way but it doesnot connect. What's wrong with my code??? also I downloaded every important thing
Your Connection URL is without a port number, add the port number to the URL
mongodb://localhost:27017/subscribers
MongoDB - default port number is 27017
https://docs.mongodb.com/manual/reference/default-mongodb-port/
https://mongoosejs.com/docs/connections.html#connections
You can connect to MongoDB with the mongoose.connect() method.
mongoose.connect('mongodb://localhost:27017/myDB', {useNewUrlParser:
true}); This is the minimum needed to connect the myapp database
running locally on the default port (27017). If connecting fails on
your machine, try using 127.0.0.1 instead of localhost.
You can also specify several more parameters in the uri:
Good evening
Since the last time that i used the sockets module, after trying to write similar code, it doesn't work.
I'm trying to create a simple client/server app.
Server accepts the connection but then the client somehow exits.
# server_host = '', server_port = 9999
connection_info = (self.server_host, self.server_port)
connection = self.sock.connect(connection_info)
But then, i can't use any socket method, since connection is None.
Looking through the docs, i couldn't find why
connect()
returns None.
Im working on localhost, using ss/netstat shows that the port is open.
What am i missing here?
I have a problem deploying a Nodejs app with a Postgresql database. The database comes from Heroku itself (Heroku Postgres add-on, hobby-dev). My app refused to connect to the database.
I found where the problem came from but I can't find a clean solution. And I think I could have misunderstood something (I'm new to Node and Heroku).
Heroku automatically gives me an environment variable DATABASE_CONFIG that includes the port:
postgres://username:password#hostname:port/databasename
Then, to connect with pg in my app, I use process.env.DATABASE_CONFIG as a connection string. I do something like:
const client = new Client({
connectionString: connectionString,
})
client.connect()
This fails to connect.
But if instead of using this environment variable, I cheat and change it, removing the port number from this connection string, it works.
I don't know why but the problem is that Heroku gives you this DATABASE_URL with the port included and you can't change it.
Did I do something wrong? Is there a clean solution to avoid that?
(because what I did is ugly as I hard-coded the DATABASE_CONFIG without the port directly in my code)
Thanks for your help!
First off, I would avoid using new Client() as this can lead to your connection being bottlenecked. Instead, use connection pooling. You can read a more indepth answer into why you want to do that here.
As for you direct issue, personally I have had trouble connecting to heroku postgres databases in the past, but here is a (basic) typical setup that works for me 99% of the time:
let pg = require('pg');
if (process.env.DATABASE_URL) {
pg.defaults.ssl = true;
}
// include an OR statement if you switch between a local dev db and
// a remote heroku environment
let connString = process.env.DATABASE_URL || 'postgresql://postgres:password#localhost:localpostgresport/yourlocaldbname';
const { Pool } = require('pg');
const pool = new Pool({
connectionString : connString
});
My first guess would be that it may have to do with ssl not being enabled. I know that has cause me problems in the past. Secondly, you want to make sure that you uses the process.env.DATABASE_URL, as environment variable should be set as the postgres connection string by Heroku.
I am running NginX, Node and Mongodb. And it seems that I can't acces the same database from a second app I am running. For example, I don't get anything back when I do:
collection.findOne({
name: someName
}, function(err, results){
// Returns no errors or results. Just stops working.
});
I can access the database perfectly fine from my first app, but not the second one.
This is the code I use to connect to the database in both apps.
Server = require('mongodb').Server,
Db = require('mongodb').Db,
db = new Db('database', new Server('localhost', 27017, { auto_reconnect: true }), { w: true });
Anyone know what the problem might be?
Edit: Does it have something to do with the subdomain or ports? Too many connections?
Edit 2 (more info):
I run mongodb with service mongodb start.
In my /etc/mongodb.conf I have bind_ip = 127.0.0.1 and dbpath=/var/lib/mongodb (rest is default)
In both my apps I run the same code to establish a connection to the database, but only the first one works (I know that because I am able to retrieve information from the database in my first app).
The apps are running on different ports. The first one is running on port 1337 and the second one runs on 3000.
You are using 'localhost' as the host name to connect to this server.
This means you will only be able to connect from the same machine that mongod is running on with that hostname.
Unless all your apps run on the same server as mongod you will need to change your connect code to use the actual hostname of the mongod server.
I usually work with mongoskin because I like to be close to the database. Usually, I do a setup with a file like db.coffee, that contains just this:
mongo = require 'mongoskin'
# either local
module.exports = mongo.db 'mongodb://localhost/database'
# or remote
module.exports = mongo.db 'mongodb://<user>:<pass>#<host>:<port>/<db>?auto_reconnect=true'
Then I use it in my other sources:
db = require 'db'
users = db.collection 'users'
# Now use the collection in handlers and middleware
This seems to work perfectly fine when I am using a local mongo server, I've had an uptime for months and it never turned out to be a problem.
However, when I am using the remote second, I get problem if the server runs longer than just a few minutes - the connection to the mongodb seems lost, despite auto_reconnect. I guess this is because the localhost connection is never closed automatically.
However this led me to thinking if I am maybe using mongoskin in a wrong way, or if there's simply a bug with the auto_reconnect?
ensure mongoskin is using the 1.0.0 or higher driver