If I have a url like
www.example.com/path#one=a1&two=a2&three=a3
How do I retrieve the variables directly from the url in node.js? Since I am using ExpressJS, I tried using req.query.variable (and req.params.variable), but that wouldn't work...
My actual problem is getting the access token from Facebook, by using the client-side authentication (https://developers.facebook.com/docs/authentication/client-side/), but the url that it redirects to is structured as so:
YOUR_REDIRECT_URI#
access_token=USER_ACCESS_TOKEN
&expires_in=NUMBER_OF_SECONDS_UNTIL_TOKEN_EXPIRES
And I don't know how to get the parameters from the url...
How do I retrieve the variables directly from the url in node.js?
Not at all, because the hash part of an URL does not get transmitted to the server.
My actual problem is getting the access token from Facebook, by using the client-side authentication [...] And I don't know how to get the parameters from the url...
You have to “get” them client-side as well.
function parseQueryString(query) {
var parsed = {};
query.replace(
new RegExp('([^?=&]+)(=([^&]*))?', 'g'),
function ($0, $1, $2, $3) {
parsed[decodeURIComponent($1)] = decodeURIComponent($3);
}
);
return parsed;
}
var url = "www.example.com/path#one=a1&two=a2&three=a3";
console.log(parseQueryString(url.split('#')[1]));
Please note that this will not attempt to detect the types of the url parameters.