ExpressJS Route Parameter with Slash

半世苍凉 提交于 2019-12-05 04:01:03

问题


Im using ExpressJS. I want pass url as parameter.

app.get('/s/:url', function(req, res) {
    console.log(req.params.url);
});

/s/sg.com                   //sg.com
/s/www.sg.com               //www.sg.com
/s/http://sg.com            //http://sg.com
/s/http://sg.com/folder     //http://sg.com/folder

How to correct the route such that everything afterr /s/ will be considered as paramenter including slashes.

Thanks


回答1:


Uh, if you want to stick a URL inside of another URL, you need to URLencode it. If you want to stick one in their raw and suffer the consequences, just use app.get('/s/*'... and then manually parse out the url with req.url.slice(3). But hear me know and believe me later, URL Encoding is the right way to do this via the encodeURIComponent that is built in to JavaScript and works in both the browser and node.js.



来源:https://stackoverflow.com/questions/17417800/expressjs-route-parameter-with-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!