many_request.js 567 B

123456789101112131415161718192021
  1. var http = require('http');
  2. var request = require('request');
  3. var server = http.createServer(function (req, res) {
  4. res.write(req.url.slice(1) + '\n');
  5. setTimeout(res.end.bind(res), 3000);
  6. });
  7. server.listen(5000, function () {
  8. var pending = 20;
  9. for (var i = 0; i < 20; i++) {
  10. var r = request('http://localhost:5000/' + i);
  11. r.pipe(process.stdout, { end: false });
  12. r.on('end', function () {
  13. if (--pending === 0) server.close();
  14. });
  15. }
  16. });
  17. process.stdout.setMaxListeners(0); // turn off annoying warnings