2011-10-14 12 views
0

Node.jsを使用してLast.fmのWebサービスへのプロキシを設定しようとしています。問題は、ws.audioscrobbler.comへのすべてのリクエストがwww.last.fmに書き直されることです。したがって、たとえば$ curl http://localhost:8000/ _api/test123301 Moved Permanentlyhttp://www.last.fm/test123に送信します。同時に$ curl http://ws.audioscrobbler.com/test123ws.audioscrobbler.comのNode.jsプロキシが301からwww.last.fmに応答します

var express = require('express'), 
    httpProxy = require('http-proxy'); 

// proxy server 
var lastfmProxy = httpProxy.createServer(80, 'ws.audioscrobbler.com'); 

// target server 
var app = express.createServer(); 
app.configure(function() { 
    app.use('/_api', lastfmProxy); 
}); 
app.listen(8000); 

は、通常の404 Not Foundを返します。私はここで何が欠けているのか、まったく間違った方法でこれに完全に近づいているのか正確にはわかりません。

答えて

2

301 Moved Permanentlyを取得した理由は、ws.audioscrobbler.comがホスト名 "localhost"のHTTP要求を取得するためです。

一つの解決策は、プロキシホスト名を書き換えてみましょうするリモートサーバーに渡す前に、「ws.audioscrobbler.com」へ:

理にかなっている
var httpProxy = require('http-proxy'); 

var lastfmProxy = httpProxy.createServer(function (req, res, proxy) { 
    req.headers.host = 'ws.audioscrobbler.com'; 
    proxy.proxyRequest(req, res, { 
    host: 'ws.audioscrobbler.com', 
    port: 80, 
    }); 
}).listen(8000); 
+0

。ありがとう! – por

関連する問題