I wrote a basic TCP client as specified in Professional Node.js.
//TCP Client
var net = require('net');
var port = 4000;
var conn;
process.stdin.resume();
(function connect() {
conn = net.createConnection(port);
conn.pipe(process.stdout, {end: false});
process.stdin.pipe(conn);
process.stdin.on('data', function(data) {
if (data.toString().trim().toLowerCase() === 'quit') {
conn.end();
process.stdin.pause();
}
});
}());
When the user enters 'quit', I want the process to end its connection to the TCP server and stop accepting input from STDIN
.
When I do this now, I get the following error:
> quit
ReferenceError: quit is not defined
And after this, the code in my if
statement is not run.
How do I fix this?
The REPL is already reading from stdin. You need to execute your script outside of the REPL so that there is nothing intercepting stdin except your script.