You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GET请求参数为空返回404错误求助(Node.js/Express+routing-controllers)

问题原因及解决方案

1. 前端模板字符串引号错误

你前端的fetch请求URL用了双引号"",但模板字符串必须使用反引号``才能解析${searchValue}变量。当前写法下,URL会被当成字面量http://localhost:3000/app/getRef/entitee/${searchValue},完全不匹配后端路由,直接导致404。

修复代码:

let searchValue = ''
result = await fetch(`http://localhost:3000/app/getRef/entitee/${searchValue}`, {method:"GET"});

2. 后端路由参数未设为可选

即使修复了前端引号,当searchValue为空时,请求URL会变成http://localhost:3000/app/getRef/entitee/,但你后端定义的路由/getRef/entitee/:searchValue要求:searchValue位置必须有非空值,空参数无法匹配该路由,依然会返回404。

修改后端路由,给参数添加?标记为可选:

@Get('/getRef/entitee/:searchValue?')
async getRefEntitee(@Req() req: any, @Res() res: any, @Param("searchValue") searchValue?: string): Promise<any> {
  // 处理searchValue为空的业务逻辑,比如返回全部数据
  if (!searchValue) {
    // 你的代码
  }
  // 其他逻辑
}

这样调整后,无论searchValue是空还是有值,请求都能正确匹配到后端路由。

内容的提问来源于stack exchange,提问作者GisCat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 08:35:18