Reputation: 1175
From Node: Up and Running the book, this example of a simple TCP chat server.
//create a new TCP server
//include net module
var net = require('net')
//create a TCP server
var chatServer =net.createServer()
//make event listener by using on() method
//when connection event happens, event listener
//will call function we gave it
chatServer.on('connection', function(client){
client.write('Hi!\n');
client.write('Bye!\n');
client.end()
})
chatServer.listen(9000)
The book says, " Node doesn’t know what kind of data Telnet sent, so Node simply stores the data as binary until we ask for it in some other kind of encoding. The sequence of letters and numbers is actually bytes in hex (see “Buf- fers” on page 70 in Chapter 4 for more on this). Each byte represents one of the letters or characters in the string “Hello, yourself”. We can use the toString() method to translate Buffer data into a regular string again if we want, or we can just pass it around as it is because TCP and Telnet understand the binary, too."
I don't see a toString()
method anywhere. Is it built-in when I make the server?
Upvotes: 0
Views: 127
Reputation: 12035
See this page: https://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end
toString
is defined on the Buffer object.
Upvotes: 1