Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
How to extract page name and authToken from url using express get route in nodejs?

I have URL like this : http://localhost:3000/pageName and I am setting my express route like below:

app.get("/:pageName",(req,res)=>{
if (authentication === true) {
   res.render(req.param.pageName}
})

Above works fine when it gets http://localhost:3000/demo.

However, when it gets this type of URL : http://localhost:3000/pageName/authToken like:

http://localhost:3000/demo/ubawfei346876jhat78gw8898ig8837yr

Route says cannot get demo/ubawfei346876jhat78gw8898ig8837yr and when I changed above code to this :

app.get("/:pageName/: authToken",(req,res) => { 
   if(authentication===true{
       res.render(req.param.pageName}
   )}

Then this type of url works fine : http://localhost:3000/demo/ubawfei346876jhat78gw8898ig8837yr

But this type of URL : http://localhost:3000/demo is not working anymore

I would like to implement something like this when url is something like this

http://localhost:3000/demo then it should redirect to:

req.params.pageName

And when url looks like this http://localhost:3000/demo/yeieyhsi736hdh then it should varify token if varified redirect to:

req.params.pageName

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can implement both routes, and if an authToken is provided, then run a check on the authToken.

However, the way you are doing it right now would cause an endless redirect, as when any request to your /:pageName is made, it would be redirected to itself, so you need to provide a response with your content from within the route, not a redirect.

app.get("/:pageName", (req, res) => {
  if (authentication) {
    let pageName = req.params.pageName;
    //Retrieve something with pageName and send it;
    res.send(pageName);
  } else {
    res.send("Not authorised");
  }
})

app.get("/:pageName/:authToken", (req, res) => {
  let authToken = req.params.authToken;
  if (isGood(authToken) && authentication) { //Fictious authToken check
    let pageName = req.params.pageName;
      //Retrieve something with pageName and send it;
      res.send(pageName);
  } else {
  res.send("Not authorised");
  }
})
over 4 years ago · Santiago Trujillo Report

0

Make your url like this http://localhost:3000/demo?token=As3ferrfmi5jr4jr4btdth54y6y6rty34t45t5y666666

app.get('/:pageName', (req, res) => {
  //you can check if pageName is correct or not
  if (req.query.token != undefined) {
    const token = req.query.token;
    // we must use strong secret, I am using "secret" for demo
    jwt.verify(token,"secret", (err,data) => {
      if (err) {
        //do something if error occurs
      } else {
        // you can also do something with data which was stored on your token
        res.render(req.params.pageName);
      }
    })
  } else if (authenticate) {
    // do something for authentication to access this else if
    res.render(req.params.pageName);
  } else {
    // you can also render some error page or send 404 status
    res.send('Something went wrong!');
  }
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!