0

I am working with an API and the API is redirecting me to localhost:3001/home

When it does, it also sends a token to use for any fetch calls. The url then looks something like:

http://localhost:3001/home#access_token=zTeQkUQkUvTHYqCcmbdWKne04DXaXWur1ZMr0GGZLM_WlVnNBQDS7-sZJZ5tEEkg4Oa2DimoF67jGXJkNzUcw4YIM0WgBVum4yQkUvTHYqCcmbdWKne04DXaXWur1ZMr0GGZLM_WlVnNBQDS7-sZJZ5tEEkg4Oa2DimoF67jGXJkNzUcw4YIM0WgBVum4yOCQWGuqfMfzK60QAHJKDNBZT4-Py6rpbIE&token_type=Bearer&expires_in=3600

I am trying to display the token just to check if i can access it but to no avail.

Can anyone tell me where im going wrong?

router.get('/home?:access_token', (req, res) => {
    const token = req.param('access_token');
    res.send(`The token is: ${token}`);
  });

Ideally, I would like the page to display:

Your token is: zTeQkUQkUvTHYqCcmbdWKne04DXaXWur1ZMr0GGZLM_WlVnNBQDS7-sZJZ5tEEkg4Oa2DimoF67jGXJkNzUcw4YIM0WgBVum4yQkUvTHYqCcmbdWKne04DXaXWur1ZMr0GGZLM_WlVnNBQDS7-sZJZ5tEEkg4Oa2DimoF67jGXJkNzUcw4YIM0WgBVum4yOCQWGuqfMfzK60QAHJKDNBZT4-Py6rpbIE

Instead, I just get:

Your token is: e
Shauna
  • 181
  • 10

3 Answers3

0

You don't use path parameters in express for query parameters. They're automatically retrieved for you by calling req.query:

router.get('/home', (req, res) => {
    const token = req.query.access_token;
    res.send(`The token is: ${token}`);
});
GoncaloNGT
  • 375
  • 1
  • 16
  • If I do it this way then I get `Your token is: undefined` – Shauna Apr 29 '20 at 10:14
  • In the example URI you've stated, you're not passing access_token as a query parameter, but as an hash. You need to replace the "#" symbol after "/home" and change it to a "?". That way, access_token is a query parameter, is automatically parsed by express, and the code above will work – GoncaloNGT Apr 29 '20 at 11:21
  • Is it possible to get the hash value from the URL do you know? – Shauna Apr 29 '20 at 13:03
  • I don't think it is, no. Check [this](https://stackoverflow.com/questions/17744003/get-url-after-in-express-js-middleware-request) thread and its accepted answer, it will explain why. In your case you should definitelly go for a query parameter instead (if that's in your control) – GoncaloNGT Apr 30 '20 at 00:55
0

It should be corrected as req.params not req.param

  router.get('/home?:access_token', (req, res) => {
    const token = req.params.access_token;
    res.send(`The token is: ${token}`);
  });

P.S: I can't put a comment. try, req.params.access_token

Lilanka
  • 79
  • 1
  • 2
  • 9
  • For me when I do `req.params()` it throws an error saying that **req.params is not a function** – Shauna Apr 29 '20 at 10:22
  • It is still e. Would the issue be because it is `/home#access_token` instead of `/home?access_token` – Shauna Apr 29 '20 at 10:39
0

You have to fix like this:

router.get('/home', (req, res) => {
    const token = req.param('access_token');
    res.send(`The token is: ${token}`);
  });

You need add "?" in your url like this: "http://localhost:3001/home?access_token=yourTokenHere"

Then you can get the token from req.param().

SangLe
  • 128
  • 6