Domains only emit "error" event and are not designed for intercepting all
event emitters messages but a way to route exception handling code. Try to
replace "ee.emit('message', 1, 2, 3);" with "throw new Error('blah')" and
you'll notice it's magically appearing in app.js mainDomain 'error' handler.
The reason this is possible is because each time async function callback is
about to be executed process.domain is set to corresponding domain and
try/catch around it calls 'error' handler.
This happens automatically for all core async ( timers, net, files ) but
you might need to add domains support manually in some other cases ( like
binary modules + libuv async )
On Wednesday, 11 June 2014 08:27:58 UTC+10, dman777 wrote:
>
> 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/2fd454e0-c4f2-4abc-8189-4128c23349fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.