proxy http request with node

2019-05-21 08:13发布

问题:

I have a nodeJs front end on one domain and a rest back end server on another domain.

I would like to be able to perform ajax requests from the front to the back. So, i intend to use a reverse proxy for that, in order to bypass the same origin policy restriction.

My back end Rest server will be secured with Basic auth or OAuth. I would like to know, if i use node-http-proxy if it is possible to send an Authorization header in the http request before proxying it, and how.

Thanks a lot.

回答1:

You have plenty of ways to accomplish that:

  • You can use node-http-proxy middlewares to set the auth headers in the reverse proxy: http://blog.nodejitsu.com/http-proxy-middlewares

  • You can require the http-proxy module, create a new instance and then use all the available events.

  • Fork node-http-proxy and patch your version http-proxy.js to include custom basic_auth;

  • Roll your own reverse proxy. With node.js it's quiet easy, check this post: http://www.catonmat.net/http-proxy-in-nodejs/

Best Regards,