The url contains all the query strings after the # key http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer
how do we access the params after #
The url contains all the query strings after the # key http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer
how do we access the params after #
var url = 'http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer';
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\#&]' + name + '=([^&#]*)');
var results = regex.exec(url);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
console.log(getUrlParameter('access_token'));
console.log(getUrlParameter('expires_in'));
console.log(getUrlParameter('token_type'));
Best practice is to use ? instead of #
So your url should be
Now you can get the query params with below method
var express = require('express');
var app = express();
app.get('/callback', function(req, res){
console.log('access_token: ' + req.query.access_token);
console.log('expires_in: ' + req.query.expires_in);
console.log('token_type: ' + req.query. token_type);
});
app.listen(3000);
Anything after the # isn't sent to the server by the browser.. you can
only parse it if the url is generated or obtained from the server. Then you can use nodes in-built url module to parse symbols in the url
# use query params symbol ? just as @iagowp mentionedYou can use the substring() method: EDIT: the string you can get from response.body. You have to use body-parser or express.json
let str = "http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer";
let index=str.indexOf("#");
let res = str.substring(index+1);
Output:
$ node server.js
access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer
?instead of#