如何在快递中将url作为"url参数"传递?

Abd*_*che 1 javascript routing url-parameters node.js express

在我的快递应用程序中,我有一个路由器在听api/shorten/:

    router.get('api/shorten/:longUrl', function(req, res, next) {
        console.log(req.params.longUrl);
    }
Run Code Online (Sandbox Code Playgroud)

当我使用类似的东西:

http://localhost:3000/api/shorten/www.udemy.com
Run Code Online (Sandbox Code Playgroud)

我得到的www.udemy.com是我所期望的.

但是当我使用时:

http://localhost:3000/api/shorten/http://www.udemy.com
Run Code Online (Sandbox Code Playgroud)

我收到404错误.

http://www.udemy.com当我访问时,我想得到req.params.parameter.

All*_*ney 12

我不确定你是否还在寻找这个问题的解决方案.也许只是为了防止其他人试图弄清楚同样的事情,这是解决问题的简单方法:

app.get('/new/*', function(req, res) {

  // Grab params that are attached on the end of the /new/ route
  var url = req.params[0];
Run Code Online (Sandbox Code Playgroud)

这样你就不必为任何被误认为是路由或目录的正斜杠而烦恼,它会在/ new /之后抓住所有内容.