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

javascript-如何获取附加的URL值?

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

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

我正在尝试使用其他URL作为我的应用程序中的数据。

例如,如果我访问 localhost:3000/https://www.google.com/robots.txt

然后,我想获取https://www.google.com/robots.txt一个参数,以便可以使用它。

我尝试了以下方法,但仅在尾随值没有斜杠的情况下才有效。

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

有没有可能获取附加URL的方法?

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

你可以/*用来获取参数,然后获取索引0以获取确切的URL

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