Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js and piping a ConnectionListener

Tags:

node.js

pipe

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);
like image 984
D R Avatar asked Jun 11 '11 10:06

D R


People also ask

What is the use of piping in node JS?

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.

What is Node JS for stack overflow?

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.

How can node js program be executed?

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.


1 Answers

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'.

like image 51
Andrey Sidorov Avatar answered Nov 11 '22 17:11

Andrey Sidorov