首页 > 解决方案 > 如何将 ?url= 更改为节点结构化路由

问题描述

所以我目前正在做以下事情。

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/http

我也明白

router.get('/:url*', (req, res) => {

还应该允许http://example.com/podcast/https://storage.googleapis.com/XXX/XXX/S01/QWAS1E1.mp3 `

标签: node.jsexpress

解决方案


*可以为您的 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');
});

推荐阅读