Duplicate call to 'open' using mongoose

1

Good, starting at node.js and mongodb I'm first with a problem that I do not know if it is such.

Having the server.js files as simple as

'use strict';
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var dbapi = require("./db/db.js");

var port = process.env.PORT || 1337;

http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');

    dbapi.connection();
}).listen(port);

and db.js with

'use strict'
var mongoose = require('mongoose');

module.exports.connection = function () {
    mongoose.connect("mongodb://localhost/protocoloDb", { useMongoClient: true });

    var db = mongoose.connection;

    db.on('error', console.error.bind(console, 'connection error:'));

    db.once('open', function () {
        console.log("Now connected!");
    });
}

The result in the console is a double 'Connection established'. Is not 'eleven' supposed to be executed only once?

Thanks

    
asked by Rafael Osuna Dominguez 17.10.2017 в 14:29
source

1 answer

0

Ok, sorry, I did not see this article before posting the question:

link

Moongose uses the native MongoDB driver below and this, in turn, uses the pooling of connections for asynchronous management. This value can be modified when creating the connection using the {server: {poolSize: 2}} option in the createconnection method.

That value was specified in my case to 2, so it showed in the mongo log that there were two open connections.

    
answered by 17.10.2017 в 14:32