Get url params and redirect to url after

i have a URL, which i want to be like this:

https://randomurl.glitch.me/<put url params here to redirect to>

the param should be redirect=url to redirect to in express
any help?

You can make a url like this
domain.com/anything
And use

app.get('/:url', (req, res)=>{
res.redirect(req.params.url)
})
1 Like

Hey @17lwinn,

2 ways.

  1. Query parameters

    Format: https://someurl.glitch.me?param1=value&param2=value

    You can use the req.query property to identify URL parameters. Example code:

app.get("/", (req, res) => {
   if (req.query.param1) {
      // parameter exists
      res.redirect(req.query.param1);
   } else {
      // fallback
      res.send("hi people");
   }
}

Example URL according to your scenario: https://randomurl.glitch.me?url=https://google.com

  1. URL parameters (like what @EddiesTech said)

    In URL parameters, you can replace the paramter (stuff1 in this example) with anything.

    Format: https://somurl.glitch.me/stuff1

app.get("/:stuff1", (req, res) => {
   if (req.params.stuff1) {
      res.redirect(req.query.stuff1);
   } else {
      // fallback
      res.send("dude! you forgot to give me a url to redirect to. we don't talk anymore like we used to do.");
   }
}

WARNING Because you are using URLs withing URLs, you might need to replace the special characters of the URL with encodeURIComponent(). More info on that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent.

Hope this helps!

1 Like