Node.js 如何通过添加其他参数重定向传入URL请求

Node.js 如何通过添加其他参数重定向传入URL请求,node.js,redirect,Node.js,Redirect,问题:我的服务器应用程序收到一个传入的HTTP请求。请求如下所示:。我需要解析这个请求,修补额外的URL参数,并调用托管的html文件。因此: => 因此客户端应该看到temp.html 代码如下: function onRequest(request,response) { if(request.method =='GET') { sys.debug("in get"); var pathName = url.parse(request.url).pathnam

问题:我的服务器应用程序收到一个传入的HTTP请求。请求如下所示:。我需要解析这个请求,修补额外的URL参数,并调用托管的html文件。因此:

=>

因此客户端应该看到temp.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()回调中看到了响应。如何将其传播到父调用函数onRequest


是否有一种更简单的方法只使用node来实现这一点?我知道有很多方法可以为静态html文件提供服务。但是,我需要将URL参数传递给temp.html,所以我不知道如何做

您必须将原始
响应
传递给
重定向URL
函数,然后让它写入响应。比如:

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);
另外,既然您已经在解析url,为什么不:

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

在异步世界中,这是一个典型的错误。您不
返回文件的值,而是将回调作为参数传递,并使用结束值执行它,如下所示:

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

只是想知道一个更简单的重定向是否可以达到以下目的:

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