0

I have to extract the access token value from the following url.

http://localhost:4001/app1/#access_token=FH2yCAcgmPjMOtKcp3DE&refresh_token=pjgTyaj

How can I fetch the full url and get the access_token value using connect in node.js? I tried using req.url and req.query to get the full url.

Keeler
  • 2,102
  • 14
  • 20
user3180402
  • 579
  • 2
  • 7
  • 16
  • possible duplicate of [Node.js: Read params passed in the URL](http://stackoverflow.com/questions/7398264/node-js-read-params-passed-in-the-url) – vittore Jan 10 '14 at 05:06
  • @vittore I already checked the above link which is related to fetch the query params. But my question is to fetch "#access_token" – user3180402 Jan 10 '14 at 05:13
  • It seems odd that the access_token is being passed in this way. Is there a reason it can't be passed as a standard URL parameter? – pgreen2 Jan 10 '14 at 19:00

3 Answers3

1

Update:

The http client removes # fragments of a url before it queries the server for the page, so the server never has access to it, that's only available to the browser.


When an agent (Browser) requests a resource from a server, the agent sends the only URI to the server (not the fragment). Instead, the agent waits for the server to send the response, then the agent processes the resource according to the document type and fragment value. [[source][1] & for more info here is a [link][2]]

So, If you are having any data in the fragment, then it is up to you to process that data (AJAX)


You can think to grab the hash at client JavaScript (`window.location.hash`) and send it to serve. In case you get string like that, here is an example:
var req_url = 'http://localhost:4001/app1/#access_token=FH2yCAcgmPjMOtKcp3DE&refresh_token=pjgTyaj';
HashKeyValueParsed_JSON = {};
require('url').parse(req_url).hash.substring(1).split('&').forEach(function (x) {
    var arr = x.split('=');
    arr[1] && (HashKeyValueParsed_JSON[arr[0]] = arr[1]);
});
console.log(HashKeyValueParsed_JSON); //Output JSON: { access_token: 'FH2yCAcgmPjMOtKcp3DE',  refresh_token: 'pjgTyaj' }

You will get output:

{ access_token: 'FH2yCAcgmPjMOtKcp3DE',
  refresh_token: 'pjgTyaj' }

I recommend you to keep all your require out of loop or any functions because it a blocking call in Node.js (even though it uses cache[here is a source link]). For more information you can read this answer.

Community
  • 1
  • 1
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
1

As per nodejs url with hash, the hash portion of a url is NOT sent to the server. You should try sending the access_token by query.

Community
  • 1
  • 1
pgreen2
  • 3,601
  • 3
  • 32
  • 59
0

The part of the url that starts with # is referred to as the fragment identifier, or hash. Parsing the url will yield the hash.

If req.url returns 'http://localhost:4001/app1/#access_token=FH2yCAcgmPjMOtKcp3DE&refresh_token=pjgTyaj', then require('url').parse(req.url) returns

{ protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'localhost:4001',
  port: '4001',
  hostname: 'localhost',
  hash: '#access_token=FH2yCAcgmPjMOtKcp3DE&refresh_token=pjgTyaj',
  search: null,
  query: null,
  pathname: '/app1/',
  path: '/app1/',
  href: 'http://localhost:4001/app1/#access_token=FH2yCAcgmPjMOtKcp3DE&refresh_token=pjgTyaj' }

So that part you are looking for is in the hash property of the parsed url: require('url').parse(req.url).hash

pgreen2
  • 3,601
  • 3
  • 32
  • 59
  • What im doing wrong here.hash value is null.
    router.get('/app1', function (req, res, next) { var fullURL = require('url').parse(req.url) console.log(fullURL) });
    – user3180402 Jan 10 '14 at 05:48
  • Can you post more of your code? I could create my own but it could be totally different. – pgreen2 Jan 10 '14 at 13:02
  • router.get('/app1', function (req, res, next) { var req_url = require('url').parse(req.url) console.log(req_url); }); Output { protocol: null, slashes: null, auth: null, host: null, port: null, hostname: null, hash: null, search: null, query: null, pathname: '/app1/', path: '/app1/', href: '/app1/' } – user3180402 Jan 13 '14 at 05:07
  • How are you accessing the URL? Through browser, wget, ...? What is the full url being entered? – pgreen2 Jan 13 '14 at 05:19
  • Accesing via browser using full url http://localhost:4001/app1/#access_token=FH2yCAcgmPjMOtKcp3DE&refresh_token=pjgTyaj – user3180402 Jan 13 '14 at 05:33