Node.js - How to redirect incoming urls by adding additional parameters
This is more of a conceptual question - so please with me.
Problem: I am receiving an incoming HTTP request to my server application. The request looks something like this: http://xyz.com?id=abc . I need to parse this request, fix additional URL parameters and call the hosted html file. So:
http://xyz.com?id=abc => http://xyz.com:8080/temp.html?id=abc&name=cdf .
So the client should see 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 to solve this problem, I can see the answer in the res.end () callback. How do I propagate this to the parent calling function onRequest?
Is there an easier way to do this using node? I know 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 that.
Any help would be greatly appreciated.
source to share
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();
}
source to share
This is a classic mistake when entering the world of asynchronous games. You are not return
a file value, you pass the callback as a parameter and execute it with the final value like this:
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);
source to share
You have to pass the original response
to your function redirectUrl
and let it write the answer. 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();
}
Call:
redirectUrl(myid, response);
Also, since you are 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);
source to share