The Node.js documentation provides an example for creating an echo server:
var net = require('net');
var server = net.createServer(function (c) {
c.write('hello\r\n');
c.pipe(c);
});
server.listen(8124, 'localhost');
What purpose does this line serve?
c.pipe(c);
pipe() method in a Readable Stream is used to attach a Writable stream to the readable stream so that it consequently switches into flowing mode and then pushes all the data that it has to the attached Writable.
Node. js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node. js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.
c1.pipe(c2)
; is a short version for
c1.on('data', function(buf) { c2.write(buf); });
(plus 'drain' event handling, pause/resume etc - see docs)
So c.pipe(c)
means 'echo data sent to c'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With