2012-05-24 168 views
2

這更多的是一個概念性問題 - 請隨身攜帶。node.js - 如何通過添加附加參數來重定向傳入的URL請求

問題:我收到一個傳入的HTTP請求到我的服務器應用程序。請求是這樣的:http://xyz.com?id=abc。我需要解析此請求,修補其他URL參數並調用託管的html文件。所以:

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

所以客戶應該看到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; 
} 

即使這是要對這個問題的一個非常愚蠢的辦法,我也看到了資源的響應。 end()回調。如何將其傳播到父調用函數onRequest?

有沒有更簡單的方法來做到這一點,只是使用節點?我知道有辦法提供靜態html文件。但是,我需要將URL參數傳遞給temp.html - 所以我不知道如何執行此操作。

任何幫助將不勝感激。

回答

11

只是想知道,如果一個簡單的重定向將達到目的:

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

謝謝 - 簡單的重定向工作。 –

+2

不客氣。請你接受答案。 – almypal

0

您必須將原始response傳遞給redirectUrl函數,並讓它寫入響應。喜歡的東西:

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

這是一個經典的錯誤異步世界。你不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);