English 中文(简体)
如何通过添加额外参数来调整收到的 URL 请求的重定向
原标题:How to redirect incoming URL requests by adding additional parameters

问题: 问题: 我从服务器应用程序收到一个收到的 HTTP 请求 。 此请求类似 : < a href=" http://example.com? id=abc" rel= " no follown noreferr" > http://example. com?id=abc 。 我需要解析此请求, 修补额外的 URL 参数, 调用主机 html 文件 。 因此 :

http://example.com?id=abc gt; >http://example.com:8080/temp.html?id=abc&name=cdf

因此客户应该看到临时.html。

这里的代码是:

function onRequest(request,response) {
if(request.method == GET ) {
        sys.debug("in get");
        var pathName = url.parse(request.url).pathname;
        sys.debug("Get PathName" + pathName + ":" + request.url);
        var myidArr = request.url.split("=");
        var myid = myidArr[1];
        //Call the redirect function
        redirectUrl(myid);
}
http.createServer(onRequest).listen(8888);

function redirectUrl(myid) {
var temp=  ;
    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 );
    res.on( data , function (chunk) {
      temp = temp.concat(chunk);
    });
    res.on( end , function(){
        return temp;
      });
    });
  req.end();
  return temp;
}

尽管这样讨论这个问题实在是太愚蠢了,但我确实看到Res.end () 回调中的反应。 如何向请求的家长调用功能传播这个功能?

使用节点是否有更简单的方法来做到这一点? 我知道有方法可以提供静态 html 文件。 但是, 我需要通过 URL 参数来临时. html - 所以我不知道如何做到这一点 。

最佳回答

仅仅想知道更简单的调整方向是否有助于实现目的:

  function onRequest(request,response) {
    if(request.method == GET ) {
       sys.debug("in get");
       var pathName = url.parse(request.url).pathname;
       sys.debug("Get PathName" + pathName + ":" + request.url);
       var myidArr = request.url.split("=");
       var myid = myidArr[1];
       var path =  http://localhost:8080/temp.html?id=  + myid +  &name=cdf ;
       response.writeHead(302, { Location : path});
       response.end();
    }
问题回答

这是一个古典错误, 当它来到 Async 世界时。 您不使用文件的值 < code> return , 将回调作为参数, 并用结尾值执行它, 如 :

function proxyUrl(id, cb) {
  http.request(options, function(res) {
    // do stuff
    res.on( data , function (chunk) {
      temp = temp.concat(chunk);
    });
    res.on( end , function(){
      // instead of return you are using a callback function
      cb(temp);
    });
}

function onRequest(req, res) {
  // do stuff
  proxyUrl(id, function(htmlContent) {
    // you can write the htmlContent using req.end here
  });
}

http.createServer(onRequest).listen(8888);

您必须将原始 reponse 转到您的 recentUrl 函数, 让它写入回复。 例如 :

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

呼吁它:

redirectUrl(myid, response);

既然你已经在分析骨髓脏了 为什么不:

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




相关问题
iis 7 relative redirect with get parameters

I have a http redirect in iis7 to send request to another domain. If url is something like http://www.example.com/news/ it s ok but if i try http://www.example.com/news/?id=3 then get parametes is ...

nginx redirect HTTPS to HTTP

How can i redireect from https to http? i have the code below but it does not seem to work. server { listen 443; server_name example.com; rewrite ^(.*) http://example.com$1 ...

How does HTTP 302 work?

How does HTTP 302 work? I would like to know the internals

How to redirect with post data (Django)

When processing a POST request in the Django views.py file, I sometimes need to redirect it to another url. This url I m redirecting to is handled by another function in the same Django views.py file. ...

热门标签