如何让Node.js后端动态接收任意数量的前端GET请求参数?
问题
希望后端能足够动态,可接收前端传来的任意数量的参数。当前用Express编写的路由仅能处理固定层级的参数路径,例如请求/api/sc/something/history可正常工作,但请求/api/sc/something/match/history会失败。尝试过简化路由匹配,但未达到预期效果,请问无需不断添加路由项,如何实现该需求?
后端现有代码
app.get(['/api/sc/:something', '/sc/:something', '/api/sc/:something/:serviceParams'], async (req, res) => { const service = req.params.service; // get the token from the header const token = req.headers.authorization.split(' ')[1]; const service_ = ((service, serviceParams) => { if(serviceParams === undefined){ return service; } return service + "/" + serviceParams })(req.params.service, req.params.serviceParams) // add the token to the bearer header of axiosInstance and then call the api const options = getOptions(service_, token, "GET"); try { const a = await axiosInstance(options); res.json(a.data) } catch (d) { if ([202, 302, 303].indexOf(d.response.status) !== -1) { // response is not ready yet, so do a get loop res.send(await getLoop(d.response.data.location, token)); } else { res.json({error: d.response.data}) } } });
前端失败请求示例
try { const historyRes = await apiClient.get("/api/sc/something/match/history"); setHistoryData(historyRes.data) console.log(historyRes, "HISTORY") } catch (error) { console.log("error", error) }
尝试过的无效路由配置
app.get(['/api/sc/:something', '/sc/:something'], async (req, res) => { const service = req.params.service; // get the token from the header const token = req.headers.authorization.split(' ')[1]; })(req.params.service, req.params.serviceParams) // add the token to the bearer header of axiosInstance and then call the api const options = getOptions(service, token, "GET"); try { const a = await axiosInstance(options); res.json(a.data) } catch (d) { if ([202, 302, 303].indexOf(d.response.status) !== -1) { // response is not ready yet, so do a get loop res.send(await getLoop(d.response.data.location, token)); } else { res.json({error: d.response.data}) } } });
解决方案
Express支持通配符参数匹配任意层级的后续路径,只需在路由参数后加*,就能捕获该位置之后的所有路径片段,无需重复添加路由项。具体修改如下:
更新路由规则
将原路由替换为带通配符的规则,合并重复前缀:app.get(['/api/sc/:something*', '/sc/:something*'], async (req, res) => { // 后续逻辑 });这里的
:something*会完整匹配/api/sc/或/sc/之后的所有内容,比如something/match/history会被全部捕获到req.params.something中。修正路径拼接逻辑
原代码中req.params.service是笔误,实际应为req.params.something,现在直接用捕获到的完整路径即可:// 获取完整的后续路径参数 const service_ = req.params.something; // 原token获取、请求逻辑保持不变 const token = req.headers.authorization.split(' ')[1]; const options = getOptions(service_, token, "GET");完整修改后的代码
app.get(['/api/sc/:something*', '/sc/:something*'], async (req, res) => { // 获取完整的后续路径参数 const service_ = req.params.something; // 从请求头获取token const token = req.headers.authorization.split(' ')[1]; // 构造请求选项 const options = getOptions(service_, token, "GET"); try { const a = await axiosInstance(options); res.json(a.data) } catch (d) { if ([202, 302, 303].includes(d.response.status)) { // 响应未就绪,执行循环获取逻辑 res.send(await getLoop(d.response.data.location, token)); } else { res.json({error: d.response.data}) } } });
关键说明
- 带
*的参数会匹配该位置之后的所有路径片段,包括多级路径,完美适配任意数量参数的需求。 - 修正了原代码中
req.params.service的笔误问题,确保参数正确获取。 - 一次配置即可覆盖所有层级的请求,无需后续新增路由规则。
内容的提问来源于stack exchange,提问作者Hein
相关产品推荐
相关产品推荐

