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