How to redirect incoming URL requests by adding additional parameters

后端 未结 3 534
慢半拍i
慢半拍i 2021-01-14 18:56

Problem: I get an incoming HTTP request to my server application. The request is something like this : http://example.com?id=abc. I need to parse this request, patch additio

3条回答
  •  春和景丽
    2021-01-14 19:25

    You have to pass the original response to your redirectUrl function, and let it write to the response. Something like:

    function redirectUrl(myid, response) {
      var options = {
        host: 'localhost',
        port: 8080,
        path: '/temp.html?id=' + myid + '&name=cdf',
        method: 'GET'
      };
      var req = http.request(options, function(res) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
    
        // Proxy the headers
        response.writeHead(res.statusCode, res.headers);
    
        // Proxy the response
        res.on('data', function (chunk) {
          response.write(chunk);
        });
        res.on('end', function(){
            response.end();
          });
        });
      req.end();
    }
    

    Calling it:

    redirectUrl(myid, response);
    

    Also, since you're already parsing the url, why not do:

    var parsedUrl = url.parse(request.url, true);
    sys.debug("Get PathName" + parsedUrl.pathname + ":" + request.url);
    //Call the redirect function
    redirectUrl(parsedUrl.query.id, response);
    

提交回复
热议问题