2011-10-14 1 views
0

Node.js를 사용하여 Last.fm의 웹 서비스에 대한 프록시를 설정하려고합니다. 문제는 ws.audioscrobbler.com에 대한 모든 요청이 www.last.fm으로 다시 작성된다는 것입니다. 예를 들어 $ curl http://localhost:8000/ _api/test123301 Moved Permanentlyhttp://www.last.fm/test123으로 보냅니다. 동시에 $ curl http://ws.audioscrobbler.com/test123에서ws.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