来自http-proxy的statusCode不正确

时间:2014-02-12 14:42:01

标签: node.js node-http-proxy

这是我编写的一个小代理程序来测试一些案例。

var httpProxy = require('http-proxy'),
    connect   = require('connect'),
    endpoint  = {
      host:   'googleeeeeee.com', // or IP address
      port:   80,
    },
    staticDir = 'public';

var proxy = new httpProxy.RoutingProxy();
var app = connect()
  .use(connect.logger('dev'))
  .use(function(req, res) {
      proxy.proxyRequest(req, res, endpoint);
      console.log("Respose is " + res.statusCode);
  })
  .use(connect.static(staticDir))
  .listen(80);
  console.log("Server started");

这使用
    "http-proxy": "0.8.x"     "connect": "2.3.x"

当我运行此程序并访问它时,statusCode为200,目标URL不存在。任何帮助非常感谢。这是How to catch 404 errors using connect framework

的延续

1 个答案:

答案 0 :(得分:0)

这是通过此代码解决的

创建了一个名为errorhandler.js的文件

module.exports = function() {
        return function(req, res, next) {
                var end = res.end;
                res.end = function(chunk, encoding){
                        res.end = end;
                        res.end(chunk, encoding);
                        console.log("Reponse code is " + res.statusCode);

                };
                next();
        }
}

然后将其包含在中间件

var httpProxy = require('http-proxy'),
    connect   = require('connect'),
    endpoint  = {
      host:   'googleeeeeee.com', // or IP address
      port:   80,
    },
    staticDir = 'public';

var proxy = new httpProxy.RoutingProxy();
var app = connect()
  .use(require('./errorHandler').errorHandler())
  .use(connect.logger('dev'))
  .use(function(req, res) {
      proxy.proxyRequest(req, res, endpoint);
  })
  .use(connect.static(staticDir))
  .listen(80);
  console.log("Server started");