Warm tip: This article is reproduced from serverfault.com, please click

How to get appended URL value? (e.g. localhost:3000/URL)

发布于 2020-11-30 03:22:04

I'm trying to use other URL to be used as a data in my app.

For example, If I visit localhost:3000/https://www.google.com/robots.txt

Then I would like to get https://www.google.com/robots.txt as a parameter so I can use it.

I tried the following approach but it only works if the trailing value has no slash.

app.get('/:id', function (req, res) {
  res.send(req.params)
})

Is there a possible way to get the appended URL?

Questioner
Zack Lee
Viewed
0
Harshana Serasinghe 2020-11-30 11:24:58

You can use /* to grab the Parameters and then get the index 0 to get the exact URL

app.get('/*', function(req, res) {
  var url = req.params[0];
  res.send(url);
});