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

求助:FreeCodeCamp的Basic Node and Express: Serve Static Assets挑战无法通过

解决FreeCodeCamp「Basic Node and Express: Serve Static Assets」挑战检测失败问题

结合你的代码和目录情况,这几个点是常见的问题根源:

  • 未启动Express服务器:你的代码仅配置了静态资源中间件,但没有启动服务器监听端口,FreeCodeCamp的检测需要服务器处于运行状态才能验证功能。补充启动代码:
    // 优先使用环境变量指定的端口,否则默认用3000
    const port = process.env.PORT || 3000;
    app.listen(port, () => {
      console.log(`Server running on port ${port}`);
    });
    
  • 确认public文件夹路径:确保public文件夹和你的入口脚本(比如server.js)处于同一目录层级,不要嵌套在其他文件夹内。
  • 验证静态资源是否可访问:在public里放一个测试文件(比如hello.txt),启动服务器后访问http://localhost:3000/hello.txt,如果能正常加载,说明静态服务配置正确,可尝试检查挑战是否要求使用特定端口或路径。

另外,别忘了先执行npm install express安装依赖包,避免因缺少依赖导致服务器启动失败。

内容的提问来源于stack exchange,提问作者Ali Semih Çamkerten

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 16:20:58