Javascript node.js-带base64的http
我是node.js的新手,一直在尝试实现base64编码。我的服务器似乎没有接收/处理base64消息。代码如下: 服务器:Javascript node.js-带base64的http,javascript,node.js,Javascript,Node.js,我是node.js的新手,一直在尝试实现base64编码。我的服务器似乎没有接收/处理base64消息。代码如下: 服务器: 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, 'ba
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/');
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/');
知道我做错了什么吗 我想出了一个解决办法。我注意到当使用
req.write(数据'base64')代码>请求从未结束。相反,我创建了一个base64编码的缓冲区,然后将其写入请求
在localhost上测试了这些精确的代码段:
客户:
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();
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();
服务器:
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/');
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/');
除此之外,我还注意到了代码中的这些错误
req.on('data',function(b) {
var content = new Buffer(b, 'base64').toString('utf8')
});
请注意,在本例中,b
实际上已经是一个缓冲区。您应该使用b.toString('base64')代码>。还要注意,b
实际上只是数据的片段。相反,您应该收集b
的数据,然后收听end
事件以最终对数据进行处理。对于req.write(数据'base64')代码>,末端永远不会触发,导致挂起而不是触发事件
以下是您收集数据的方式:
var content = '';
req.on('data', function(b) {
content += b;
});
req.on('end', function() {
//do something with content
});
看看这个: