[英]How do I serve an HTML file in Express from a directory which is a level above my server folder?
I am trying to serve an HTML file which is in my main directory, through my Express server which is one level deeper: in my server folder.我正在尝试通过更深一层的 Express 服务器提供位于我的主目录中的 HTML 文件:在我的服务器文件夹中。 This is how the server code is set up:
这是服务器代码的设置方式:
const express = require('express'); const app = express(); app.use(express.json()); app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); }) const port = 5000; app.listen(port, () => console.log(`Server is listening on port ${port}.`))
The error I am receiving is:我收到的错误是:
Error: ENOENT: no such file or directory, stat '/mnt/c/Project-Directory/server/index.html'
It is clear from this error why I am not able to access my HTML file;从这个错误中可以清楚地看出为什么我无法访问我的 HTML 文件; my server exists in the /server folder and my index.html file exists in the Project-Directory folder.
我的服务器存在于 /server 文件夹中,我的 index.html 文件存在于 Project-Directory 文件夹中。 However, I am not sure how to access my HTML file in its current directory.
但是,我不确定如何访问当前目录中的 HTML 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.