0

I am working on a project that has a mobile appplication, and two web servers(let it be named tub and cloud). THe web servers are developed in node.js environment.

The mobile app will request for some information from the web server named 'tub' ;

this 'tub' will in turn act as a http client and request some information from the web server named 'cloud'.

When the 'tub' requests the 'cloud', the 'cloud' has to process the incoming request and then has to do header redirection to another url(possibly in the 'cloud' or some other server) and return the response back to the 'tub'.

I am facing certain difficulty in figuring out header redirection in the 'cloud' server. Below I have mentioned the coding for putting a rest api call from 'tub' to '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();

Below mentioned code is in the 'cloud' server for header redirection :

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

I am not getting neither any response nor any error in the 'tub' . Am I going wrong anywhere ? How can we track whether header redirection is taking place or not.

Ginu George
  • 1
  • 1
  • 4

1 Answers1

0

Express can handle the redirection response automatically, using res.redirect('/foo/bar');

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

It's inadvisable to set both the Location and Content-Disposition headers together: Content-Disposition with 302 redirect

Community
  • 1
  • 1
James Thomas
  • 4,303
  • 1
  • 20
  • 26
  • I have tried as mentioned above but to no luck. I am getting just the following chunk of data at the 'tub' web server : " Moved Temporarily. Redirecting to http://10.0.0.1:80/mobilelib.zip " – Ginu George Apr 17 '15 at 09:58
  • You have to handle that redirection. Your tub client is not showing response body because there isn't I've. – Zlatko Apr 18 '15 at 06:18
  • @zlatko Sorry, I didn't get you properly. The redirected url has the zip file present , so it has to throw the response. – Ginu George Apr 27 '15 at 07:14