使用nodejs中httpProxy代理时候出现404异常的解决方法
2016-08-15 09:28
776 查看
在公司中使用nodejs构建代理服务器实现前后台分离,代码不能拿出来,然后出现httpProxy代理资源的时候老是出现404.明明被代理的接口是存在的。代码大概如下:
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给删除,改进代码如下:
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);
然后如期运行正确。
以上这篇使用nodejs中httpProxy代理时候出现404异常的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- 使用nodejs中httpProxy代理时候出现404异常
- 关于使用Git的时候出现"HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large"的解决方法
- Web项目中使用java2word出现异常 解决方法
- eclipse中配置tomcat 以及运行eclipse中tomcat的时候访问http://localhost:8080出现404错误的解决方法
- 遇到的错误-----MySQL使用临时表 出现 “ERROR 1137 (HY000): Can't reopen table” 的异常 解决方法
- 使用安装完的IIS出现HTTP500错误的解决方法(2003中)
- PocoDataMySQL与Boost::asio共同使用时出现“用户回调期间未处理异常”解决方法
- https使用HttpWebRequest出现错误:System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.的解决方法
- nodejs 使用app.use(express.bodyParser()); 出行异常----解决方法
- 使用getHibernateTemplate()的方法时出现空指针异常的解决办法
- 用apxs 安装apache mod_proxy时候出现undefined symbol: ap_proxy_lb_workers错误的解决方法
- 使用MySQL和Hibernate时,出现java.lang.UnsupportedOperationException: Update queries only supported through HQL异常的解决方法
- Exchange 2007 OWA中出现“HTTP 错误404”解决方法
- TransactionProxyFactoryBean 代理事务 $Proxy5 cannot be cast to异常解决方法
- 使用javamail出现java.net.SocketException: Network is unreachable: connect异常 解决方法
- IIS6 下使用ASP.NET 4.0时浏览网站出现404错误的解决方法
- 使用yum安装软件出现“The requested URL returned error: 404”解决方法
- windows出现系统故障或无法启动的时候可以使用的6种解决方法
- 新建ASP.NET项目的时候出现HTTP/1.1 500 Server Error的错误解决方法
- 关于在使用tomcat做开发测试的某些时候无端报ClassNotFound异常的解决方法