I am new to Node.js domains and I am experimenting with them. I am trying 
to get mainDomain to display a message on database that is successful. 
Right now, there are no errors and everything runs ok when I start 
express.js. But the message does not show as it should after a successful 
database connection. I  can successfully get it to catch event error if I 
use ee.emit('error') instead of ee.emit('message', 1, 2, 3);. I don't 
understand why I can't emit ee.emit('message') and have it catch it.

var express = require('express');
var morgan = require('morgan');
var fs = require("fs");
var createDomain = require("domain").create;
var app = express();

var db = require(__dirname + '/app/data/db.js');
var mainDomain = new createDomain();
mainDomain.run(function () { 
    db.connectDatabase(); 
    mainDomain.on('message', function(msg) {
        console.log('yoyoyoyo');
    });
});
mainDomain.run(function () {
    mainDomain.on('error', function(er) {
        console.log('error, but oh well', er.message);
    });
    var server = app.listen(9000, function() {
            console.log('Listening on port %d', server.address().port);
    });
});



-----------------------------------------------------------------------------------------------------------------------

db.js:


var mongoose = require( 'mongoose' );
var EventEmitter = require('events').EventEmitter;
var ee = new EventEmitter();

function connectDatabase() {
    var dbURI = 'mongodb://localhost/database';
    mongoose.connect(dbURI);
    mongoose.connection.on('connected', function () {
        ee.emit('message', 1, 2, 3);
       // console.log('Mongoose default connection open to ' + dbURI);
    });
    mongoose.connection.on('error',function (err) {
        console.log('Mongoose default connection error: ' + err);
    });
    mongoose.connection.on('disconnected', function () {
        console.log('Mongoose default connection disconnected');
    });
    process.on('SIGINT', function() {
        mongoose.connection.close(function () {
            console.log('Mongoose default connection disconnected through' +
                        ' app termination');
            process.exit(0);
        });
    });
}   
module.exports.connectDatabase = connectDatabase;

-- 
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/4754b5cf-5969-4150-9b25-7c5d7fac7597%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to