无法在Netlify上使用Node.js将 HTML文件发送给前端

前端开发 2026-07-09

我想用Netlify托管一个使用Express的 Node.js应用。

This is my server.js code:

const express = require("express")
const path = require("path")
const bodyParser = require("body-parser")

const app = express()

app.use(bodyParser.urlencoded({extended: false}))


app.get("/", (req, res) => {
    res.sendFile(path.join(process.cwd(), "index.html"))
})
app.get("/js", (req, res) => {
    res.sendFile(path.join(process.cwd(), 'index.js'))
})
app.get("/css", (req, res) => {
    res.sendFile(path.join(process.cwd(), 'index.css'))
})

app.get("/data", (req, res) => {
    res.sendFile(path.join(process.cwd(), "data.html"))
})


data = {}

app.post("/data/input", (req, res)=>{
    let dataPkg = req.body

    data[dataPkg.id] = dataPkg.name

})

app.post("/data/output", (req, res)=>{

    res.send(data)

})


module.exports = {app};

This is my netlify.toml:

[build]
    publish = "dist"
    functions = "netlify/functions"
[functions]
    external_node_modules = ["express", "path", "body-parser"]
[[redirects]]
    force = true
    status = 200
    from = "/*"
    to = "/.netlify/functions/index"

And here's how the directory looks:

directory img

我使用Netlify CLI部署了站点,但在尝试打开时,对任意文件都会出现:

错误:ENOENT:没有这样的文件或目录,stat '/var/task/data.html'

我该怎么办?

解决方案

现在,你的HTML文件(index.html和 data,html)位于 src,而它们应该在 dist,因为Netlify会在 dist 中查找它们。

请将所有前端文件移动到 dist.

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章