微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用nodejs中httpProxy代理时候出现404异常的解决方法

在公司中使用nodejs构建代理服务器实现前后台分离,代码不能拿出来,然后出现httpProxy代理资源的时候老是出现404.明明被代理的接口是存在的。代码大概如下:

rush:js;"> 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);

然后报错或者是404错误码。

解决方案:

在代理请求中,把请求头中的host给删除,改进代码如下:

rush:js;"> var http = require('http'),res) {

delete req.headers.host;
proxy.web(req,{ target: 'http://www.cnblogs.com/xiaopen/' });
});

console.log("listening on port 5050")
server.listen(5050);

然后如期运行正确。

以上这篇使用nodejs中httpProxy代理时候出现404异常的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

原文地址:https://www.jb51.cc/nodejs/46595.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐