【问题标题】:Node.js proxy for ws.audioscrobbler.com responds with 301 to www.last.fmws.audioscrobbler.com 的 Node.js 代理以 301 响应 www.last.fm
【发布时间】:2011-10-14 23:43:39
【问题描述】:

我正在尝试使用 Node.js 为 Last.fm 的网络服务设置代理。问题是对 ws.audioscrobbler.com 的每个请求都被重写为 www.last.fm。例如$ curl @987654321@_api/test123 发送一个301 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 @987654322@ 返回一个普通的404 Not Found。我不确定我在这里遗漏了什么,或者我是否完全错误地处理了这个问题。

【问题讨论】:

    标签: api redirect node.js proxy last.fm


    【解决方案1】:

    您收到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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2016-11-23
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多