Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Express router for accepting a URL as route parameter

Imagine you want to build a webpage health checker. I want users to send GET requests to https://localhost/check/{valid_url_here} and I send a request to that URL. I couldn’t find proper way of writing the correct router for this in Express.

This is the closest answer but it trims the url after ? adding it to req.query

router.get('/check/:url*', (req, res) => {})

so when URL is https://example.com/?print=1 the variable req.params.url misses the ?print part… I want to capture anything that comes after /check/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

router.use("/check", function(req, res) {
  var url = req.url.substring(1);
});

Then https://localhost/check/https://example.com/?print=1 leads to url = "https://example.com/?print=1".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading