Node.js代理能够更改响应头并注入其他请求数据

ali*_*ona 6 api node.js node-http-proxy

我正在编写node.js代理服务器,为不同域上的API提供请求.

我想使用node-http-proxy,我已经找到了修改响应头的方法.

但是,有没有修改的条件请求数据的方式(即加入API密钥),并考虑到可能有不同的方法要求- ,GET,POST,?UPDATEDELETE

或者也许我搞砸了node-http-proxy的目的,有什么更适合我的目的?

exp*_*nit 3

一种使其变得非常简单的方法是使用中间件。

var http = require('http'),
    httpProxy = require('http-proxy');

var apiKeyMiddleware = function (apiKey) {
  return function (request, response, next) {
    // Here you check something about the request. Silly example:
    if (request.headers['content-type'] === 'application/x-www-form-urlencoded') {
        // and now you can add things to the headers, querystring, etc.
        request.headers.apiKey = apiKey;
    }
    next();
  };
};

// use 'abc123' for API key middleware
// listen on port 8000
// forward the requests to 192.168.0.12 on port 3000
httpProxy.createServer(apiKeyMiddleware('abc123'), 3000, '192.168.0.12').listen(8000);
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息以及该方法的一些注意事项,请参阅Node-HTTP-Proxy、Middlewares 和 You 。