简体   繁体   English

如何从高于我的服务器文件夹级别的目录在 Express 中提供 HTML 文件?

[英]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 文件。

You can use path.join and specify that you want one directory back ( ../ ):您可以使用path.join并指定要返回一个目录( ../ ):

const path = require('path');


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Webpack时,我的服务器可以静态服务于哪个文件夹? - When using Webpack, which folder do I have my server statically serve? 如何配置我的 Express 服务器以呈现我的所有内容,而不仅仅是我的 HTML 文件? - How do I configure my Express server to render all of my content, not just my HTML file? 我如何强制 express 为没有文件扩展名且内容类型为“text/html”的静态 html 文档提供服务 - How do i force express to serve static html documents having no file extentions with 'text/html' content type 如何在express.js中提供从服务器引用js文件并在客户端运行它的html? - How to serve html in express.js that references js file from server and run it on client side? 如何使用Express服务部分动态HTML页面? - How do I serve partially dynamic HTML pages with Express? 如何使用快递服务目录? - how to serve a directory with express? 如何使用Node Express提供静态文件? - How do I serve a static file using Node Express? 当 multer 是 localhost 时,如何将文件从 multer 发送到托管在服务器上的网站文件夹 - How can i send a file from multer to my website folder which is hosted on a server while multer is localhost 尝试从前端加载.tsv文件。 该文件保留在我的服务器上。 如何使用Express做到这一点? - Trying to load a .tsv file from the front end. The file stays on my server. How do I do this using Express? 由于路径相对性问题,无法从快递服务器提供angular 2 index.html文件 - Cannot serve angular 2 index.html file from express server due to path relativity issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM