gpt4 book ai didi

node.js - Express应用程序找不到index.html

转载 作者:太空宇宙 更新时间:2023-11-04 00:18:49 27 4
gpt4 key购买 nike

当我将 Express 应用程序推送到 Tessel 2 时,我遇到了问题,我可以在本地正常工作,但一旦将其推送到另一个系统上,就会出现问题。这是我收到的错误:

Error: ENOENT: no such file or directory, stat '/tmp/remote-script/public/index.html'
at Error (native)

这是我的 Express 路线设置:

app.use('/static', express.static('public'))

app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
})

我尝试了几个不同的文件路径位置,例如/../public/index.html,但它们似乎都不起作用。我的目录结构将 index.js 作为主 Node 文件,然后我有一个公共(public)目录,其中包含 index.html 文件。

任何帮助将不胜感激。

最佳答案

尝试在express.static中使用绝对路径

app.use('/static', express.static(path.join(__dirname, 'public')))

然后

res.sendFile(path.join(__dirname, '/public/index.html'));

此外,我建议您应该使用 render 来代替上面的内容

res.render('index')

关于node.js - Express应用程序找不到index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45403029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com