1

Is it possible somehow to redirect to same route with query params ?

User hitting url: localhost:3000

I want to modify it to be: localhost:3000/?something=somethingvalue

Tried res.redirect but ofcourse I'm getting too many requests error, as I'm creating endless loop. Googled the solution but with no luck, maybe wording was wrong what I'm trying to achieve.

Is this even possible ?

Thanks

Polisas
  • 491
  • 1
  • 9
  • 20

3 Answers3

1

You might be able to add a simple check for if something is empty:

app.get('/', function(req, res) {

    // Checking if the "something" GET parameter is undefined or isn't at least 1 character long
    if(req.query.something === undefined || req.query.something.length < 1) {

        res.redirect('/?something=somethingvalue');
    }
});
robere2
  • 1,689
  • 2
  • 16
  • 26
0

Does this naive solution work well enough?

app.get("/", (req, res) => {
  if (req.query.something != undefined) {
    // do something
  } else {
    return res.redirect("/?something=somethingvalue")
  }
})
notme
  • 424
  • 5
  • 14
0
app.get('/', function(req, res) {

    // Checking if the "something" GET parameter is undefined or isn't at least 1 character long
    if(req.query.something === undefined || req.query.something.length < 1) {

        res.redirect('/?something=somethingvalue');
    }
});
4b0
  • 21,981
  • 30
  • 95
  • 142
Milan
  • 1