http.createServer((req, res) => { req.on('data', (chunk) => { const canContinue = res.write(chunk.toString().toUpperCase()) if (!canContinue) { req.pause() res.once('drain', () => req.resume()) } }) req.on('end', () => res.end()) })
http.createServer(function(req,res){ res.writeHead(200,{'Content-Type':'text/plain'}); function doAgain(){ var isWritable = true; while(1){ if(chance.bool({likelihood:99})){ isWritable = res.write(chance.string({length:(16*1024)-1})); if(!isWritable){ console.log('backpressure'); res.once('drain',doAgain); break; } }else{ res.end('This is the end'); break; } } } doAgain(); }).listen(8080,function(){ console.log('listening on 8080'); })
return res.once('drain', generateMore)
http .createServer((req, res) => { res.writeHead(200, { "Content-Type": "text/plain" }); function generateMore() { while (chance.bool({ likelihood: 95 })) { let shouldContinue = res.write( chance.string({ length: 16 * 1024 - 1 }) ); if (!shouldContinue) { console.log("backpressure"); return res.once("drain", generateMore); } } res.end("\nThe end \n", () => { console.log("All data was sent"); }); } generateMore(); }) .listen("8082", () => { console.log("Listening on 8082"); });