NodeJs解码为可读文本
问题
我想通过NodeJs使用IP地址从设备接收数据。 但是我收到了以下数据:
我所试过的
这是我已经能够得到的代码,仍然产生我上面描述的问题。
var app = require('http').createServer(handler); var url = require('url') ; var statusCode = 200; app.listen(6565); function handler (req, res) { var data = ''; req.on('data', function(chunk) { data += chunk; }); req.on('end', function() { console.log(data.toString()); fs = require('fs'); fs.appendFile('helloworld.txt', data.toString(), function (err) { if (err) return console.log(err); }); }); res.writeHead(statusCode, {'Content-Type': 'text/plain'}); res.end(); }
下面是我收到的结果console.log(req.headers)
所以我的问题是,我如何解码数据? 谁知道他们是什么types的数据?
使用Buffers
来处理八位字节stream。
function handler (req, res) { let body=[]; req.on('data', function(chunk) { body.push(chunk); }); req.on('end', function() { body = Buffer.concat(body).toString('utf8'); ...