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

无法将EJS文件渲染为HTML,访问/store.html报Cannot GET错误怎么解决?

问题解决:无法访问/store.html的原因及修复方案

核心问题

你的Express应用仅配置了/store这个GET路由,但你访问的是/store.html,服务器找不到对应路由处理逻辑,因此返回"Cannot GET /store.html"。

解决方案

方案一:访问正确路由(推荐)

直接在浏览器地址栏输入 http://localhost:3000/store,即可访问渲染后的EJS页面。你的代码中已通过res.render('store.ejs')正确处理该路由,会自动将EJS模板渲染为HTML返回。

方案二:添加/store.html路由(若需保留.html后缀)

如果确实需要支持/store.html访问路径,可添加对应路由。为避免代码重复,建议将渲染逻辑抽成复用函数:

const renderStorePage = (req, res) => {
  fs.readFile('items.json', (error, data) => {
    if (error) {
      res.status(500).end()
    } else {
      res.render('store', {
        items: JSON.parse(data)
      })
    }
  })
}

// 同时支持两个访问路径
app.get('/store', renderStorePage)
app.get('/store.html', renderStorePage)

注:因为已设置view engine: 'ejs',res.render中可以省略.ejs后缀,直接写'store'即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 01:45:03