node.js 如何通过添加其他参数来重定向传入的 URL 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10737372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect incoming URL requests by adding additional parameters
提问by Pradeep Banavara
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 additional URL parameters and call a hosted html file. So:
问题:我的服务器应用程序收到一个传入的 HTTP 请求。请求是这样的:http: //example.com?id=abc。我需要解析这个请求,修补额外的 URL 参数并调用一个托管的 html 文件。所以:
http://example.com?id=abc=> http://example.com:8080/temp.html?id=abc&name=cdf.
http://example.com?id=abc=> http://example.com:8080/temp.html?id=abc&name=cdf。
So the client should see temp.html
所以客户端应该看到 temp.html
Here's the code:
这是代码:
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;
}
Even though this is a really stupid way of going about this issue, I do see the response in the res.end() callback. How to propagate this to the parent calling function onRequest ?
尽管这是解决此问题的非常愚蠢的方法,但我确实在 res.end() 回调中看到了响应。如何将此传播到父调用函数 onRequest ?
Is there a simpler way of doing this just using node ? I know that there are ways to serve static html files. However, I need to pass URL parameters to temp.html - so I'm not sure how to do this.
是否有一种更简单的方法可以仅使用 node 来做到这一点?我知道有一些方法可以提供静态 html 文件。但是,我需要将 URL 参数传递给 temp.html - 所以我不确定如何执行此操作。
回答by almypal
Just wondering if a simpler redirect would serve the purpose:
只是想知道更简单的重定向是否可以达到目的:
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();
}
回答by alessioalex
This is a classical error when coming to the async world. You don't returnthe value of the file, you pass a callback as a parameter and execute it with the end value like so:
这是进入异步世界时的经典错误。您不知道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);
回答by Linus Gustav Larsson Thiel
You have to pass the original responseto your redirectUrlfunction, and let it write to the response. Something like:
您必须将原始文件传递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();
}
Calling it:
调用它:
redirectUrl(myid, response);
Also, since you're already parsing the url, why not do:
另外,既然您已经在解析 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);

