I use node.js, and have the next code:
var server = net.createServer(function(socket) {
socket.on('data', function(d) {
console.log(d);}}
I see that it prints the next:
<Buffer 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0..>
Even if I open a conection in my localhost. What can be the reason? How I change it that it would get the http request?
According to the docs, you must specify the encoding with the setEncoding()
method to receive an string.
If you don't, you'll receive an Buffer with raw data because Node will not know what encoding you want - and Node deals with very low level networking.
For example:
var server = net.createServer(function(socket) {
socket.setEncoding("utf8");
socket.on('data', function(d) {
console.log(d); // will be string
}
}
If you don't want to, you can always call toString()
:
var server = net.createServer(function(socket) {
socket.on('data', function(d) {
console.log(d.toString());
}
}
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