2014-02-12 64 views
0

這是我寫的一個小型代理程序,用於測試某些情況。來自http代理的statusCode不正確

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

回答

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"); 
相關問題