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

NestJS中video/:fileName路由未映射问题求助

NestJS 中 video/:fileName 路由未映射的解决方案
  • 确认控制器前缀与完整路由路径
    从日志 [RoutesResolver] FileController {/file} 可知,FileController 的基础路由前缀为 /file,因此 @Get('video/:fileName') 对应的完整路由是 /file/video/:fileName,而非根路径下的 /video/:fileName。如果直接访问 /video/:fileName 会提示路由不存在,需使用完整路径测试。

  • 验证代码与编译文件一致性
    确保修改后的代码已保存,并且项目的编译目录(通常是 dist)已更新。可手动删除 dist 目录后重新编译启动,避免旧编译文件导致的路由未更新问题。

  • 修复潜在语法错误
    viewVideo 方法中 const videoPath = join(process.cwd(), fileName) 缺少分号,尽管 JavaScript 支持自动分号插入,但在 TypeScript 环境下可能影响装饰器解析。补上分号后重启应用:

    @Get('video/:fileName')
    viewVideo(@Res() res: Response, @Param('fileName') fileName: string): string {
        const videoPath = join(process.cwd(), fileName);
        return fileName;
    }
    
  • 排查 @Res() 装饰器的影响
    使用 @Res() 会将响应控制权完全交给 Express,可能导致 Nest 的路由扫描逻辑异常。暂时移除 @Res() 装饰器测试路由是否能正常映射:

    @Get('video/:fileName')
    viewVideo(@Param('fileName') fileName: string): string {
        const videoPath = join(process.cwd(), fileName);
        return fileName;
    }
    
  • 检查全局路由前缀配置
    查看 main.ts 中是否设置了全局路由前缀:

    app.setGlobalPrefix('file');
    

    若存在该配置,所有路由都会自动添加 /file 前缀,需使用 /file/video/:fileName 访问。

  • 排查模块路由配置
    确认是否在模块中使用 RouterModule.forRoot() 进行路由重映射,导致 video/:fileName 路由被覆盖或未被正确注册。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 19:03:20