【问题标题】:Node-http-proxy: Requests on ressources get redirected to index.htmlNode-http-proxy:对资源的请求被重定向到 index.html
【发布时间】:2015-03-06 13:40:53
【问题描述】:

我在使用带有 node-http-proxy 模块的 node.js 时遇到了问题。 在某些网页上没有加载资源,因为服务器将请求重定向到原始 html 文件。所以对于每一个图片或者 css 文件,都会重新加载 index.html 文件。奇怪的是,这并不是在所有网站上都会发生,而只是在一些网站上发生。 这是我的代码:

var express = require('express'),
    httpProxy = require('http-proxy'),
var router = express.Router();
var proxy = new httpProxy.createProxyServer();

router.get('/:url', function(req, res) {
    var options = {
        target: {
            host: req.params.url,
            port: 80,
            path: 'http://'
        },
        timeout: 5000,
        proxyTimeout: 5000,
        xfwd: true,
        prependPath: true
    };
    proxy.web(req, res, options);
});

这不起作用的网页示例http://www.kicker.de 我已经尝试了几个选项,例如 hostRewrite 或 changeOrigin,但这些似乎并没有改变这一点。

【问题讨论】:

    标签: javascript node.js http-proxy


    【解决方案1】:

    我强烈建议您使用 npm 模块 request。我对其他通过 http 和 https 通过重定向获取文件的方法有疑问。 request 处理得很好。这是我必须获取内容的一些代码。

    var request = require("request");
    
    ...
    
    function fetchImage(url, callback) {
        var file, tmpFile;
    
        async.waterfall([
            function (next) {
                tmp.file(next);
            },
            function (location, descriptior, cleanupCallback, next) {
                console.log(location);
                tmpFile = location + ".png"
                console.log("fetching", url, "and saving it as", tmpFile);
                var download = request(url).pipe(fs.createWriteStream(tmpFile));
    
                download.on("finish", function(){
                    console.log("finished downloading picture", tmpFile);
                    fs.stat(tmpFile, next)
                });
            }
        ], callback);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多