在我的 express 应用程序中,我有一个监听 api/shorten/
的路由器:
router.get('api/shorten/:longUrl', function(req, res, next) {
console.log(req.params.longUrl);
}
当我使用类似的东西时:
http://localhost:3000/api/shorten/www.udemy.com
我得到了 www.udemy.com
,这正是我所期望的。
但是当我使用时:
http://localhost:3000/api/shorten/http://www.udemy.com
我收到 404 错误。
我想在访问 req.params.parameter
时获取 http://www.udemy.com
。
最佳答案
我不确定您是否仍在寻找解决此问题的方法。也许以防万一其他人试图弄清楚同样的事情,这是解决您问题的简单方法:
app.get('/new/*', function(req, res) {
// Grab params that are attached on the end of the /new/ route
var url = req.params[0];
这样您就不必担心任何正斜杠被误认为是路由或目录,它会抓取/new/之后的所有内容。
关于javascript - 如何在 express 中将 url 作为 "url parameter"传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43007961/