[nodejs] redirect/intercept stdout within node

2,863 views
Skip to first unread message

Dominic Tarr

unread,
Dec 5, 2010, 5:31:17 PM12/5/10
to nod...@googlegroups.com
hi,

I'm trying to figure out how to capture the stdout produced within a block of code, say, to test that the stdout is correct.

although this seems fairly straightforward to do is the likes of ruby, I can't figure out how to do it in nodejs

can anyone give me any clues?


Preston Guillory

unread,
Dec 5, 2010, 6:42:13 PM12/5/10
to nod...@googlegroups.com
process.stdout.write = (function(write) {
    return function(string, encoding, fd) {
        write.apply(process.stdout, arguments)
    }
})(process.stdout.write)

It's a global hook, so if you only want to apply it to a certain block it would be on you to remove it after the block finishes.  Also note that technically it could accept either a string or a buffer.

Preston Guillory

unread,
Dec 5, 2010, 6:53:31 PM12/5/10
to nod...@googlegroups.com
https://gist.github.com/729616

Fleshed it out a bit.

Dominic Tarr

unread,
Dec 5, 2010, 10:29:11 PM12/5/10
to nod...@googlegroups.com
thanks, preston.

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Reply all
Reply to author
Forward
0 new messages