【问题标题】:Header Redirection in a Cloud server that stands apart from the main application server与主应用程序服务器不同的云服务器中的标头重定向
【发布时间】:2015-04-17 05:43:03
【问题描述】:

我正在开发一个项目,该项目有一个移动应用程序和两个 Web 服务器(命名为 tub 和 cloud)。 Web 服务器是在 node.js 环境下开发的。

移动应用将向名为“tub”的网络服务器请求一些信息;

这个“浴缸”将反过来充当一个 http 客户端,并从名为“云”的网络服务器请求一些信息。

当“浴缸”请求“云”时,“云”必须处理传入的请求,然后必须将标头重定向到另一个 url(可能在“云”或其他服务器中)并返回响应回到“浴缸”。

我在弄清楚“云”服务器中的标头重定向时遇到了一定的困难。下面我提到了将rest api调用从'tub'到'cloud'的编码:

var options =
{
    hostname: '10.0.0.1',
    port: 1048,
    path: '/api/v1/vendor?DevType=mobile,
    method: 'GET',
    headers:
    {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
};

var request = http.request(options, function(cloudres)
{
    cloudres.setEncoding('utf8');

    cloudres.on('data', function (chunk)
    {
        console.log("chunk");
        console.log(chunk);
    });
});

request.on('error', function(e)
{
    console.log('Cloud Package http Request Error : ' + e.message);
});

request.end();

下面提到的代码在“云”服务器中用于标头重定向:

var express = require('express');
var http = require('http');

var app = express(); // Create an Express instance.

var httpserver = http.createServer(app); // Create a http server and hook the Express instance to it.

httpserver.listen(1048,function() // Make the http server listen to a port.
{
    console.log("Http Server Listening On Port : 1048");
});

app.get('/api/v1/vendor',function(req,res) 
{
    res.statusCode = 302;
    res.setHeader('Content-Disposition', 'attachment');
    res.setHeader('Location', 'http://10.0.0.1:80/mobilelib.zip');
    res.end();
});

我在 'tub' 中既没有得到任何响应,也没有得到任何错误。我在哪里出错了吗?我们如何跟踪是否发生了标头重定向。

【问题讨论】:

    标签: node.js http-headers url-redirection


    【解决方案1】:

    Express 可以自动处理重定向响应,使用 res.redirect('/foo/bar');

    app.get('/api/v1/vendor',function(req,res) 
    {
        res.redirect('http://10.0.0.1:80/mobilelib.zip');
    });
    

    不建议同时设置 Location 和 Content-Disposition 标头:Content-Disposition with 302 redirect

    【讨论】:

    • 我已经尝试过如上所述但没有运气。我在“浴缸”网络服务器上只得到以下数据块:“暂时移动。重定向到10.0.0.1:80/mobilelib.zip
    • 您必须处理该重定向。您的浴缸客户端没有显示响应正文,因为我没有。
    • @zlatko 对不起,我没有正确理解你。重定向的 url 存在 zip 文件,因此它必须抛出响应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多