2

I'm struggling to figure out how to get my "access_token" params values from my URL in Node js script, I know by default this would use "?" but I need to make sure that it picks up "#" instead

 example.com/#access_token=asdad32423&somethingelse=1232
    
const request = require("request");
const queryString = require("query-string");
const cookieParser = require("cookie-parser");
const app = express();
const port = 4000;

    app.get("/", (req, res) => {
      const accessToken = req.query["#access_token"];
      console.log(req.query);
      res.cookie("access_token", accessToken);
    
    });
   app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});
mmz
  • 31
  • 6

2 Answers2

0

That part of the URL is called the fragment, and it would not be sent to the server. If you need the data, you must extract it using js and send it as a query parameter or the inside the body of the request using ajax or a form.

Amir MB
  • 3,233
  • 2
  • 10
  • 16
  • Let's say i extract it on the client side, then how I send the values back to my node? – mmz Dec 07 '22 at 23:52
  • Using ajax: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch – Amir MB Dec 07 '22 at 23:53
  • Ajax? I need to have the value extracted on the client side and send to my node...what am I even pointing the endpoint to? Doesn't seem right... I'm not fetching anything – mmz Dec 07 '22 at 23:59
  • @mmz on the client-side you should extract it and send it to another endpoint that you need to create. You can not read it in the same request – Amir MB Dec 08 '22 at 00:04
0

The fragment identifier as in http://somedomain.com/somePath#someHash is not sent to the server. This is kept local for the browser.

Values added to a URL that are to be sent to the server should be added a query parameter using ? instead of'#.

http://example.com/?access_token=asdad32423&somethingelse=1232

Then, you can access them in Express with req.query.access_token.

jfriend00
  • 683,504
  • 96
  • 985
  • 979