zoukankan      html  css  js  c++  java
  • 使用nodejs中httpProxy代理时候出现404异常

    在公司中使用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);

    然后如期运行正确。

    http://stackoverflow.com/questions/24465675/http-proxy-keeps-returning-404

  • 相关阅读:
    2.2 Scala基础知识
    Linux---用户及权限管理类命令
    Linux---进程控制类命令
    Linux---vim编辑文本文件
    Linux---文件压缩与解压缩命令
    Linux---查找命令
    Linux---基本目录与文件命令
    nginx配置技巧汇总
    Go 内嵌静态资源
    go语言的time.Sleep
  • 原文地址:https://www.cnblogs.com/xiaopen/p/5769294.html
Copyright © 2011-2022 走看看