Summary of http-proxy usage in Nodejs

Source: Internet
Author: User

Recently in writing Xmocker tools, used in the development of pre-mock data, the inevitable use of agent middleware. Of course, there are middleware on GitHub for the Http-proxy package. After all, it is their own practiced hand project, wrote a middleware, to facilitate the customization of functions.

The Http-proxy library is used in KOA and is the Proxy.web method for using it. The general usage is:

function (e) {...});

The requirement in the project is to delegate the API to the user-filled URL. KOA provides the Req and res, the user provides the URL, so the middleware is easy to write

 function   Proxyto ({status}) { return  async function   (CTX, next) { return  Next (). Then (async () =& Gt  { if  (status = = = Undefined | | ctx.status = = =  status) {Let data  try          {data  = await Proxy.web (Ctx.req, Ctx.res)  catch   (e) { if   (ERR) Console.log (err)  ret Urn   

Set the access result to 404 for proxy or not proxy. Then use this function in the middleware of KOA.

The basic application is naturally possible, but when it comes to the post, it is not, because the use of koa-bodyparser, resulting in post requests can not be sent to the server. Looking for a solution in Http-proxy's issue, I finally found a way to get req to re-stream.

function (Proxyreq, req, res, options) {    if  (req.body) {      = json.stringify (req.body)       // incase if Content-type is application/x-www-form-urlencoded, we need to change to Application/json      Proxyreq.setheader (' Content-type ', ' Application/json ')      proxyreq.setheader (' content-length ' ) , Buffer.bytelength (bodydata))      //  stream the content      proxyreq.write ( Bodydata)    }  })

Well, this basically completes a proxy middleware.

It's good to start with, at least the normal API can be forwarded. So try to proxy to the company's API system, the Web site also found no problem. Then with the Fiddler Agent Android Client, with the regular proxy link to their services, there is garbled ... The client is using the Okhttp issued by the request, Fiddler on the back of the JSON data is clearly normal, the results on the mobile phone Chinese is not correct, are hexadecimal code, there is no resolution. Careful comparison: Coding? It's all utf-8, this is fine. GZIP: I used the pipe in the past, the code is intact. I tried to capture all the data, after Gzip decoding, is also correct, then why is it wrong?

I haven't found any problems since I've been communicating with others for a long time. There's no way to find it in the code. For the requested header, it seems that the cookie appears to be one less, just look at the header forwarding time how to handle. It turns out that the header is all converted. Issue the question that mentions Set-cookie, I This is also this situation, try to look at this file change record, unexpectedly found an option item: Preserveheaderkeycase.

The header in the Nodejs is in lowercase, so the conversion is made in the Http-proxy, and the entire header of the request is converted to lowercase. So I changed the true so that it didn't change. Then, the client is OK!!! Haha, a closer look at the cookie is still not more than one. Forget it, regardless of the cookie, at least normal login no problem.

Add a changeorigin to true to prevent some server path to take the initial domain name, there is a 404 problem. So much for the time being. The address of the proxy plugin is:

Https://github.com/wenlonghuo/xmocker/blob/master/app/plugin/proxy.js

You are welcome to try my mock tool:

Https://github.com/wenlonghuo/xmocker-cli

Summary of http-proxy usage in Nodejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.