Hi Oleg,
Thank alot for the reply, but as i have mentioned earlier i am very new to
nodejs world and does not understand most of its features, i tried finding
online resources but those are limited or very basic either very advance.
nevertheless, Oleg i haven't yet tried reading what is express.js and
neither i am looking for any 3rd party adins/package to achieve this basic
database connection functionality. moreover i am looking for best practice
what real node developers follows to encounter such issues. curious to know
if they create new connection on every user request or they do share
connections.
and also if you can help me with a very basic and minimal code example of
sharing database connection object, thought-out modules will be a great
help for me.
moreover not just db connection object but if you can put some light on
common practice of sharing instance of objects throughout projects using
modules/requires would be much much appreciated.
Thanks for writing this.
Manish Bansal
On Tuesday, 2 September 2014 17:35:34 UTC+5:30, Manish Bansal wrote:
>
> Hi developers,
> I am new to node.js and mongodb and trying to use native mongodb driver
> for node to read database.
> now my very basic example works good, but this is not i really want to
> achieve at the end of the day.
>
> //Testing installation
>
> var MongoClient = require('mongodb').MongoClient;
> MongoClient.connect('mongodb://127.0.0.1:27017/testDB', function(err, db)
> {
> var collection = db.collection('testTable');
> collection.find().toArray(function(err, results) {
> console.log(results);
> db.close();
> });
> });
>
> I want to serve my data via http through node server, if on every
> operation i would be closing the connection, then i would be ended up
> creating thousands of connections simultaneously, and Main objective to
> choose node+mongodb is performance and real-time (of-course i will be using
> ws to achieve real-time objectives) information distribution system goes
> for a toss.
>
>
> // file dbconnection.js
>
> var MongoClient = require('mongodb').MongoClient;
> MongoClient.connect('mongodb://127.0.0.1:27017/testDB', function(err, db)
> {
> //Now here i dont want to close the db connection but want to reuse
> exports.getConnection = db;
> });
> });
>
>
> // file TestConnection.js
> var dbconnection = require('./dbconnection.js')
> var db = dbconnection.getConnection;
>
>
> //Error on next line (basically db object is null)
> var collection = db.collection('testTable');
> collection.find().toArray(function(err, results) {
> console.dir(results);
> });
>
>
> I am very much confuse how i can use require funcationality effectively to
> pass object instance across project. one i can think of present scenario is
> creating db connection instance on the main server.js page where i am
> creating http object and pass this object to function call.
>
> Please advice me what are the best possible ways to achieve such
> requirements. so that I will create main instance only once and keep on
> using it for my every request come via node HTTP server.
>
> Thanks in advance,
> Manish Bansal
> [email protected]
>
>
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/3e3db47a-d41d-4b4d-9fc1-e109159cf073%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.