In the company, we use nodejs to build a proxy server to achieve front-end and back-end separation. The code cannot be taken out. Then, when httpProxy proxy resources appear, 404 always appears. The interface being proxyed exists. The code is roughly 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);Then report an error or a 404 error code.
Solution:
In the proxy request, delete the host in the request header, and the improved code is 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);Then run correctly as scheduled.
The solution to the above article when using httpProxy proxy in nodejs is the entire content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.