I am a greenhorn in node.js. I need to get singleton DB object or connection pool in order to send multiple requests. Currently I'm using the following code:
var http = require('http'),
io = require('/usr/local/lib/node_modules/socket.io'),
server = http.createServer(function(req, res)
{
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello world</h1>');
});
server.listen(1337);
var socket = io.listen(server);
socket.on('connection', function(client)
{
Init();
});
var dataBaseSource = 'mongodb://127.0.0.1:27017/GameDatabase';
var MongoClient = require('/usr/local/lib/node_modules/mongodb').MongoClient, format = require('util').format;
var dataBase;
function Init()
{
MongoClient.connect(dataBaseSource, function (error, db)
{
if (error)
{
throw error;
}
else
{
dataBase = db;
}
});
}
After applicaton started console throws an error:
info - socket.io started
debug - client authorized
info - handshake authorized lhDeq1WSfXUpiIlb06fv
debug - setting request GET /socket.io/1/websocket/lhDeq1WSfXUpiIlb06fv
debug - set heartbeat interval for client lhDeq1WSfXUpiIlb06fv
debug - client authorized for
debug - websocket writing 1::
/usr/local/lib/node_modules/mongodb/lib/mongodb/mongo_client.js:409
throw err
^
Error: failed to connect to [127.0.0.1:27017]
at null.<anonymous> (/usr/local/lib/node_modules/mongodb/lib/mongodb/connection/server.js:546:74)
at emit (events.js:106:17)
at null.<anonymous> (/usr/local/lib/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15)
at emit (events.js:98:17)
at Socket.<anonymous> (/usr/local/lib/node_modules/mongodb/lib/mongodb/connection/connection.js:533:10)
at Socket.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:419:13)
Can you adjust my snapshot or provide an example.
Many thanks.