2

Ok, this one has to be a very easy question, but I just started learning node. I am also new to javascript, so, please show no mercy on pointing out wrong directions below.

In particular I have two files:

  • one has a class that creates some slave servers in different ports
  • the other one is the "main" file that generates the slaves

When I attempt to print out what I have just initialized I get two weird errors:

  • connections property is deprecated. Use getConnections() method, and then
  • it crashes when I attempt to apply JSON.stringify in the new object (Converting circular structure to JSON)

Code for slaves in file "slave.js":

var http = require ("http");

function Slave () {
}

Slave.prototype.ID           = undefined;
Slave.prototype.coordinator  = false;
Slave.prototype.httpServer   = undefined;
Slave.prototype.thePort      = undefined;

Slave.prototype.isCoordinator = function () { return this.coordinator; }

/*****************************************************************/

function handle_incoming_request (req, res) {
    console.log("INCOMING REQUEST: " + req.method + " " + req.url);
    res.writeHead (200, { "Content-Type" : "application/json" });
    res.end( JSON.stringify({ "error" : null }) + "\n" );
}

exports.createSlave = function (id, coordinatorK, port) {
    var temp         = new Slave ();
    temp.ID          = id;
    temp.coordinator = coordinatorK;
    temp.thePort     = port;
    temp.httpServer  = http.createServer(handle_incoming_request);
    temp.httpServer.listen (temp.thePort);
    console.log ("Slave (" + (temp.isCoordinator() ? "coordinator" : "regular") + ") with ID " + temp.ID + " is listening to port " + temp.thePort);
    console.log ("--------------------------------------------");

    return temp;
}

Now, the main file.

var http   = require ("http");
var url    = require ("url");
var a      = require ("./slave.js");

var i, temp;
var myArray = new Array ();

for (i = 0; i < 4; i++) {
    var newID = i + 1;
    var newPort = 8000 + i + 1;
    var coordinatorIndicator = false;

    if ((i % 4) == 0) {
        coordinatorIndicator = true; // Say, this is going to be a coordinator
    }

    temp = a.createSlave (newID, coordinatorIndicator, newPort);
    console.log ("New slave is  : " + temp);
    console.log ("Stringified is: " + JSON.stringify(temp));

    myArray.push(temp);
}
1
  • 3
    You can't stringify just any old object. Commented Sep 10, 2013 at 18:15

2 Answers 2

3

You're trying to stringify the result of http.createServer(...). That'll not be what you want to do, so when you create that property, make it non-enumerable by defining it using Object.defineProperty().

exports.createSlave = function (id, coordinatorK, port) {
    var temp         = new Slave ();
    temp.ID          = id;
    temp.coordinator = coordinatorK;
    temp.thePort     = port;

    Object.defineProperty(temp, "httpServer", {
        value: http.createServer(handle_incoming_request),
        enumerable: false, // this is actually the default, so you could remove it
        configurable: true,
        writeable: true
    });
    temp.httpServer.listen (temp.thePort);
    return temp;
}

This way JSON.stringify won't reach that property curing its enumeration of the object.

Sign up to request clarification or add additional context in comments.

2 Comments

It is true that this one is working, with a minor change: the command temp.httpServer.listen (temp.thePort); should be after the Object.defineProperty(...). Please change this order. I will wait for a few more minutes to see if there is another answer before I accept it. Thank you for your time!
Sure, I changed it. Didn't notice the order when I was editing.
2

Issue is the with the property httpServer of temp object having circular reference. You can either set it non enumerable using Ecmascript5 property definision as mentioned in the previous answer or you can use the replacer function of JSON.stringify to customize it not to stringify the httpServer property.

   console.log ("Stringified is: " + JSON.stringify(temp, function(key, value){
         if(key === 'httpServer') return undefined;
        return value;
    }));

2 Comments

Thank you PSL for your time and the alternate explanation. I appreciate it. I am granting this one on user2736012 because he is lower on reputation though.
@MightyMouse You are welcome.. Ideally in node world you should go with EcmaScript5 definitions which will make it more standard way of defining objects. Since it was already posted i just provided an alternate.. You chose the right answer.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.