Polisas
Polisas

Reputation: 541

Redirecting to same route with params

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

Upvotes: 1

Views: 2834

Answers (3)

Milan
Milan

Reputation: 1

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');
    }
});

Upvotes: 0

robere2
robere2

Reputation: 1716

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');
    }
});

Upvotes: 1

notme
notme

Reputation: 444

Does this naive solution work well enough?

app.get("/", (req, res) => {
  if (req.query.something != undefined) {
    // do something
  } else {
    return res.redirect("/?something=somethingvalue")
  }
})

Upvotes: 0

Related Questions