2012-03-20 79 views
1

我沒有得到node-http-proxy的最後一個版本的工作(雖然這個版本在以前的版本中工作)。 Node.js版本是0.6.12,node-http-proxy版本是0.8.0。 基本上,我有一個服務器監聽端口10000和另一個監聽端口5000.我想代理最後一個人所有以/ api開頭的請求(刪除此前綴後)。與expressjs一起使用node-http-proxy時出錯

我的代碼是:

var express = require("express"); 
var httpProxy = require('http-proxy'); 
var cluster = require('cluster'); 
var fs = require('fs'); 

// Use http server for local tests 
var app = express.createServer(); 

// Proxy request targeting API 
app.all('/api/*',function(req, res){ 

    // Remove '/api' part from query string 
    req.url = '/' + req.url.split('/').slice(2).join('/'); 

    // Create proxy 
    var proxy = new httpProxy.HttpProxy(); 
    proxy.proxyRequest(req, res, { 
    target: { 
     host: 'localhost', 
     port: 5000 
    } 
    }); 
}); 

// Handle static files 
app.use(express.static(__dirname + '/public')); 

// Run application 
app.listen(10000); 

靜態文件送達正確,但是當涉及到代理的東西,我得到這個錯誤:

Error: Both `options` and `options.target` are required. 
at new <anonymous> (/Users/luc/Projects/test/www/node_modules/http-proxy/lib/node-http-proxy/http-proxy.js:53:11) 
at /Users/luc/Projects/test/www/server.js:15:16 
at callbacks (/Users/luc/Projects/test/www/node_modules/express/lib/router/index.js:272:11) 
at param (/Users/luc/Projects/test/www/node_modules/express/lib/router/index.js:246:11) 
at pass (/Users/luc/Projects/test/www/node_modules/express/lib/router/index.js:253:5) 
at Router._dispatch (/Users/luc/Projects/test/www/node_modules/express/lib/router/index.js:280:4) 
at Object.handle (/Users/luc/Projects/test/www/node_modules/express/lib/router/index.js:45:10) 
at next (/Users/luc/Projects/test/www/node_modules/express/node_modules/connect/lib/http.js:203:15) 
at Object.handle (/Users/luc/Projects/test/www/node_modules/express/lib/http.js:83:5) 
at next (/Users/luc/Projects/test/www/node_modules/express/node_modules/connect/lib/http.js:203:15)-error-undefined 

我已經添加了目標的要求但仍然是同樣的錯誤。

回答

0

此外,你可能會被想說var proxy = new httpProxy.RoutingProxy();

相關問題