ExpressJS路由参数带斜线

ExpressJS路由参数带斜线

本文介绍了ExpressJS路由参数带斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ExpressJS。我想要传递url作为参数。

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

如何更正路线,以便所有afterr / s / 将被视为参数,包括斜杠。

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

谢谢

推荐答案

如果你想粘一个URL在另一个网址内,您需要对其进行URL编码。如果你想粘在一个原始的并且遭受后果,只需使用 app.get('/ s /*'...然后手动解析出url与 req.url.slice(3)。但是听到我的消息后,相信我的URL编码是通过 encodeURIComponent ,内置于JavaScript,并在浏览器和node.js中都可以使用。

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.

这篇关于ExpressJS路由参数带斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:35