javascript - Data loss in Node.js child process -
i'm trying send data (as object) child process in node.js, however, of regular expressions lost in transfer.
var arguments = { something: { name: 'test', age: 28, active; true }, otherthing: 'some string', regex: /test/i, regex2: new regexp('test') }; var child = cp.fork(path.join(__dirname, 'child.js')); child.on('message', function (data) { console.log(data); }); child.send(arguments);
in child.js file have @ top:
process.on('message', function () { console.log(arguments); // data has changed });
when log output child process arguments object instead looks this:
{ something: { name: 'test', age: 28, active: true }, otherthing: 'some string', regex: {}, regex2: {} }
so far unable find elsewhere why may happening, ideas?
because separate javascript processes, can't send objects. when pass object, gets serialized json , parsed child. (see the docs.)
json not support serializing regex objects. (try putting json.stringify(/abc/)
through console -- "{}"
.)
to include regexes in json object, can use json-fn
module. supports serializing functions, dates, , regexes. (it issue raised added regex support. :))
you like:
var jsonfn = require('json-fn'); var arguments = { something: { name: 'test', age: 28, active; true }, otherthing: 'some string', regex: /test/i, regex2: new regexp('test') }; var child = cp.fork(path.join(__dirname, 'child.js')); }); child.send(jsonfn.stringify(arguments));
and:
var jsonfn = require('json-fn'); process.on('message', function (data) { console.log(jsonfn.parse(data))); // data has changed });
Comments
Post a Comment