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

Node.js Express项目访问/login.html出现Cannot GET错误的问题咨询

错误原因分析及解决办法

核心问题:路由路径不匹配

你的Express服务器仅配置了/和/login两个GET路由:

app.get('/',(req,res)=>{
    res.sendFile(__dirname+'/static/index.html');
})

app.get('/login',(req,res)=>{
    res.sendFile(__dirname+"/static/"+"login.html");
})

但index.html里的跳转链接写的是./login.html,点击后浏览器会向/login.html路径发送请求,服务器没有对应路由处理这个请求,因此返回Cannot GET /login.html错误。

额外问题:表单提交路径同样不匹配

login.html里的表单action设置为login.html,提交时会向/login.html发送POST请求,但你的服务器仅配置了/login的POST路由,后续提交也会出错。

解决步骤

  1. 修改index.html的跳转链接:
<a href="/login">Please Login Here!</a>
  1. 修改login.html的表单提交路径:
<form action="/login" method="post">
  1. (可选优化)使用Express静态文件中间件托管static目录,无需手动为每个静态页面写路由:
    在server.js开头添加:
app.use(express.static('static'));

添加后可直接访问/index.html和/login.html,同时不影响原有路由逻辑。

内容的提问来源于stack exchange,提问作者Theow Meng Ang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:55:30