The solution to the 404 exception occurs when using the Httpproxy agent in Nodejs _node.js

Source: Internet
Author: User

In the company using NODEJS to build proxy server to achieve the separation of the front and back, the code can not be taken out, and then appear httpproxy agent resources always appear 404. The interface is clearly represented. The code is probably as follows:

var http = require (' http '),
  httpproxy = require (' Http-proxy ');

var proxy = Httpproxy.createproxyserver ({});

var server = Http.createserver (function (req, res) {
 
 
 proxy.web (req, res, {target: ' http://www.cnblogs.com/xiaopen/') });
});

Console.log ("Listening on port 5050")
Server.listen (5050);

And then an error or 404 wrong code.

Solution:

In the proxy request, remove the host in the request header and improve the code as follows:

var http = require (' http '),
  httpproxy = require (' Http-proxy ');

var proxy = Httpproxy.createproxyserver ({});

var server = Http.createserver (function (req, res) {
 
 delete req.headers.host;
 Proxy.web (req, res, {target: ' http://www.cnblogs.com/xiaopen/'});
}

; Console.log ("Listening on port 5050")
Server.listen (5050);

And then run correctly as scheduled.

The above use of the Nodejs httpproxy agent when 404 unusual solution is to share all the content of the small, hope to give you a reference, but also hope that we support the cloud habitat community.

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.