2

I'm a node.js newbie stuck trying to implement base64 encoding. My server doesn't seem to receive/process the base64 message. Code below:

Server:

var http = require('http');
http.createServer(function (req, res) {
  req.on('data',function(b) {
    console.log("HEY!"); // <--- Never gets called
    var content = new Buffer(b, 'base64').toString('utf8')
    console.log("CLIENT SAID: "+content);
    var msg = JSON.parse(content);
    // do stuff and respond here...
  });
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Client:

var http = require('http');
var options = {
  hostname : 'localhost',
  port     : 1337,
  method   : 'POST'
};
var req = http.request(options, function(res) {
  res.setEncoding('base64');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});
req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
var msg = {'name':'Fred','age':23};
var msgS = JSON.stringify(msg);
req.write(msgS,'base64');
req.end();

Any ideas what I'm doing wrong?

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Greg
  • 10,696
  • 22
  • 68
  • 98

1 Answers1

2

I came up with a fix. I noticed when using req.write(data, 'base64'); that the request never ended. I instead created a buffer that was base64 encoded, then wrote it to the request.

These exact snippets were tested localhost:

Client:

var http = require('http');
var options = {
  hostname: 'localhost',
  port: 1337,
  method: 'POST'
};
var req = http.request(options, function (res) {
  res.setEncoding('base64');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

var msg = {
  'name': 'Fred',
  'age': 23
};
var msgS = JSON.stringify(msg);
var buf = new Buffer(msgS, 'base64');

req.write(msgS);
req.end();

Server:

var http = require('http');
http.createServer(function (req, res) {
  var content = '';
  req.on('data', function (chunk) {
    content += chunk;
  });
  req.on('end', function() {
    content = content.toString('base64');
    console.log(content);
    //content returns {"name": "Fred","age": 23};

    res.end();
  });
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Aside from those things, I noticed these errors in your code.

req.on('data',function(b) {
  var content = new Buffer(b, 'base64').toString('utf8')
});

Note that b in this case is actually already a buffer. You should use b.toString('base64');. Also note that b is actually only fragments of the data. You should instead collect the data of b, then listen to the end event to finally do something with the data. In your case with req.write(data, 'base64');, the end would never fire, leading to a hangup instead of the event firing.

This is how you'd collect data:

var content = '';
req.on('data', function(b) {
  content += b;
});
req.on('end', function() {
  //do something with content
});
hexacyanide
  • 88,222
  • 31
  • 159
  • 162