Ask coding questions

← Back to all posts
Node.js - Redirect based on URL/Query Parameters
h
Retrospicer (10)

I basically just want to redirect the user to a different site based on the url (or query)parameters but I have no idea how.

If the url is https://example.com/redirect?link=https://example.com then it should redirect to https://example.com.

If the url is https://example.com/redirect?link=https://stackoverflow.com then it should redirect to https://stackoverflow.com and so on (Redirect to any site in parameters).

How would I do this? (In Node, JS, HTML, e.t.c.)

Thanks in advance!

Answered by Coder100 (18109) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18109)

sure, its easy in js

app.get("/redirect", (req, res) => {
  res.redirect(req.query.link);
});

but this only assumes that the user actually created a ?link query.

Retrospicer (10)

@Coder100 Thanks! Before redirects were /redirect/https://site.com
but now they are /redirect?link=https:/site.com

Coder100 (18109)

nice
but both are invalid??
no link can have //? @TamoghnaK13

Retrospicer (10)

@Coder100 Can you help me here if possible (How-to-only-display-output-in-a-bash-repl)
https://replit.com/talk/ask/How-to-only-display-output-in-a-bash-repl/138779 Thanks! @Coder100