Node.js 如何将?url=更改为节点结构化路由
因此,我目前正在做以下工作Node.js 如何将?url=更改为节点结构化路由,node.js,express,Node.js,Express,因此,我目前正在做以下工作 http://example.com/podcast?url=https://storage.googleapis.com/radiomediapodcast/QWA/S01/QWAS1E1.mp3 但是,我想做以下几点 http://example.com/podcast/https://storage.googleapis.com/XXX/XXX/S01/QWAS1E1.mp3 我知道,router.get('/:url',(req,res)=>{ 应该允许我寄
http://example.com/podcast?url=https://storage.googleapis.com/radiomediapodcast/QWA/S01/QWAS1E1.mp3
但是,我想做以下几点
http://example.com/podcast/https://storage.googleapis.com/XXX/XXX/S01/QWAS1E1.mp3
我知道,router.get('/:url',(req,res)=>{
应该允许我寄
我也明白这一点
router.get('/:url*', (req, res) => {
还应允许“,
*
允许为您的url调用路由,req.params
将允许您读取传递的内部url
使用http://example.com/podcast/https://storage.googleapis.com/XXX/XXX/S01/QWAS1E1.mp3
router.get('/prodcast/*', async (req, res, next) => {
console.log(req.query.params); //https://storage.googleapis.com/XXX/XXX/S01/QWAS1E1.mp3
res.status(200).send('ok');
});
定义你喜欢的路由器
app.get('/podcast/:url(*)', (req, res) =>
:url(*)
而不是:url*
现在,您可以通过req.params.url
获取url
您可以使用自己的方式(:url*
),但需要一些技巧来获取url数据,在这种情况下,req.params
如下
{
0: "//example.com/podcast/https://storage.googleapis.com/XXX/XXX/S01/QWAS1E1.mp3",
url: "http:"
}
然后,url数据将是:constURL=req.params.url+req.params[0];