{"size": 818, "ext": "js", "max_stars_repo_path": "nodejs-in-practice/chapter7/http-proxy.js", "max_stars_repo_name": "jiangshanmeta/notebook", "max_stars_repo_stars_event_min_datetime": "2019-12-06T16:50:17.000Z", "max_stars_repo_stars_event_max_datetime": "2019-12-06T16:50:17.000Z", "max_issues_repo_path": "nodejs-in-practice/chapter7/http-proxy.js", "max_issues_repo_name": "jiangshanmeta/notebook", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "nodejs-in-practice/chapter7/http-proxy.js", "max_forks_repo_name": "jiangshanmeta/notebook", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.3870967742, "max_line_length": 60, "alphanum_fraction": 0.5415647922}
const http = require('http');
const url = require('url');

http.createServer((req,res)=>{
    console.log('start request:',req.url);
    const options = url.parse(req.url);
    options.headers = req.headers;
    // 作为客户端请求
    const proxyReq = http.request(options,(proxyRes)=>{
        proxyRes.on('data',(chunk)=>{
            console.log('proxy length',chunk.length);
            res.write(chunk,'binary');
        });
        proxyRes.on('end',()=>{
            console.log('proxy end');
            res.end();
        });

        res.writeHead(proxyRes.statusCode,proxyRes.headers);
    });

    req.on('data',(chunk)=>{
        console.log('in request length',chunk.length);
        // 转发请求
        proxyReq.write(chunk,'binary');
    });
    req.on('end',()=>{
        proxyReq.end();
    });

}).listen(8000);