第三方包_http proxy - dkvirus/npm-resource-read GitHub Wiki

说明

http-proxyhttp-proxy-middleware 一样都是处理 http 代理,目前版本 v1.11.1。

$ npm install http-proxy --save

示例

var http = require('http'),
    httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000); // See (†)

//
// Create your target server
//
http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

当访问 localhost:8000 时会代理到 localhost:9000