Created
January 21, 2013 07:10
-
-
Save guange2015/4584139 to your computer and use it in GitHub Desktop.
用来做手机流量限制,以模拟慢速的2G网络。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fs = require('fs'); | |
| var http = require('http'); | |
| var util = require('util'); | |
| var url = require('url'); | |
| http.createServer(function (req, res) { | |
| var options = url.parse(req.url); | |
| var req_headers = req.headers; | |
| req_headers["connection"] = "close"; | |
| var req_options = { | |
| hostname: options.hostname, | |
| port: options.port || 80, | |
| path: options.path || "/", | |
| method: req.method || "GET", | |
| headers: req_headers | |
| } | |
| console.log(req_options); | |
| var remote_req = http.request(req_options, function (remote_res) { | |
| console.log(remote_res.headers); | |
| res.writeHead(remote_res.statusCode, remote_res.headers); | |
| remote_res.on('data', function (data) { | |
| console.log(''+data.length); | |
| res.write(data); | |
| remote_res.pause(); | |
| setTimeout(function () { | |
| remote_res.resume(); | |
| },1000); | |
| }) | |
| remote_res.on('end', function () { | |
| res.end(); | |
| }) | |
| //remote_res.pipe(res); | |
| }); | |
| remote_req.on('error', function (e) { | |
| console.log("problem with request " + e.message); | |
| }); | |
| req.pipe(remote_req); | |
| //res.end(util.inspect(req)); | |
| }).listen(8080); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment