0

我正在尝试使用 Node.js 为 Last.fm 的网络服务设置代理。问题是对 ws.audioscrobbler.com 的每个请求都被重写为 www.last.fm。因此,例如发送一个to 。$ curl http://localhost:8000/_api/test123301 Moved Permanentlyhttp://www.last.fm/test123

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);

同时返回一个正则. 我不确定我在这里缺少什么,或者我是否完全错误地处理了这个问题。$ curl http://ws.audioscrobbler.com/test123404 Not Found

4

1 回答 1

2

你得到 a 的原因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);
于 2011-10-17T03:46:58.077 回答