# sendProxyRequest
return new Promise(function(resolve, reject) {
var protocol = Container.proxy.requestModule;
var proxyReq = protocol.request(reqOpt, function(rsp) {
var chunks = [];
rsp.on('data', function(chunk) { chunks.push(chunk); });
rsp.on('end', function() {
Container.proxy.res = rsp;
Container.proxy.resData = Buffer.concat(chunks, chunkLength(chunks));
resolve(Container);
rsp.on('error', reject);
koa中使用pipe
Can't pipe stream to ctx.res
ctx.body = ctx.req.pipe(request(`https://www.google.com/?q=${ctx.query.q}`));
价值:可以用来转发
buffer带来性能提升
Node如直接返回String,需要一个转换,这样性能比较低,可以直接使用buffer
Node JS Buffer使用理解:一个例子,提升了177%
Node.js学习之路04——Buffer对象与字符串
var buf = new Buffer('BUFFER OBJECT');
console.log(buf);
console.log(buf.toString());
console.log(buf.toString('utf8', 2, 5));
proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
res.end('Something went wrong. And we are reporting a custom error message.');
// 另外新建一个 HTTP 80 端口的服务器,也就是常规 Node 创建 HTTP 服务器的方法。
// 在每次请求中,调用 proxy.web(req, res config) 方法进行请求分发
var server = require('http').createServer(function(req, res) {
// 在这里可以自定义你的路由分发
var host = req.headers.host, ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log("client ip:" + ip + ", host:" + host);
switch(host){
case 'aaaa.com':
case 'bbs.aaaa.com':
proxy.web(req, res, { target: 'http://xyd.bbbbb.com:82' });
break;
case 'vps.cccc.com':
proxy.web(req, res, { target: 'http://xyd.bbbbb.com:8080' });
break;
case 'dddd.com':
case 'www.dddd.com':
proxy.web(req, res, { target: 'http://localhost:81' });
break;
case 'eeeeee.com.cn':
case 'www.eeee.com.cn':
proxy.web(req, res, { target: 'http://eeeee.com.cn:8082' });
break;
default:
res.writeHead(200, {
'Content-Type': 'text/plain'
res.end('Welcome to my server!');
console.log("listening on port 80")
server.listen(80);
const request = require('request');
request('http://www.google.com', function (error, response, body) {
console.error('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
node-http-proxy
http.createServer(function(req, res) {
proxy.web(req, res, { target: 'http://mytarget.com:8080' });
koa-proxy
var koa = require('koa');
var proxy = require('koa-proxy');
var app = koa();
app.use(proxy({
host: 'http://alicdn.com'
app.listen(3000);
流式返回结果,有一篇问答,和自己预期的结果一样
koa.js streaming response from remote url
var req = require('request');
//...
this.body = req(url);
app.use('/', (req, res) => {
req.pipe(request.post({ url: 'http://www.example.com', form: { foo: 'bar' }}), {end: false}).pipe(res);
app.listen(process.env.PORT || 3000);
host: 'proxy.xxxx.com', // 这里是代理服务器
port: 8080, // 这里是代理服务器端口
path: request.url,
method: request.method,
headers: {
// 如果代理服务器需要认证
'Proxy-Authentication': 'Base ' + new Buffer('user:password').toString('base64') // 替换为代理服务器用户名和密码
var req = http.request(options, function(req, res) {
res.pipe(response); // 这个pipe很喜欢
console.log(req.url);
}).end();
}).listen(8080);
测试如上代码不可用