chunk是一个Buffer对象 data += chunk;// 隐藏的toString() }) .on("end", function () { //对data转码 }); 17 // 正确的方法 var chunks = []; var size = 0; res.on('data', function (chunk) { chunks.push(chunk); size += chunk.length; }); res.on('end', function () { var data = null; switch(chunks.length) { case 0: data = new Buffer(0); break; case 1: data = chunks[0]; break; default: data = new Buffer(size); for (var i = 0, pos = 0, l = chunks.length; i < l; i++) { var chunk = chunks[i]; chunk.copy(data, pos); pos += chunk.length; } break; } // 简单且正确的方法 var bufferHelper = new BufferHelper(); req.on("data", function (chunk) { bufferHelper.concat(chunk); }) .on('end', function () { var html = bufferHelper.toBuffer().toString(); });
• 进程与消息 var cluster = require('node-cluster'); var master = new cluster.Master(); master.register(8080, 'app.js'); master.dispatch(); var http = require('http'); var cluster = require('node-cluster'); var worker = new cluster.Worker(); var server = http.createServer(function (req, res) { // server }); worker.ready(function (socket) { server.emit('connection', socket); }); 19 负载均衡 多核利用 提升稳定