ENOENT:没有这样的文件或目录,Express Middleware的stat错误 [英] ENOENT: no such file or directory, stat ERROR with Express Middleware

查看:194
本文介绍了ENOENT:没有这样的文件或目录,Express Middleware的stat错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是文件路径的常见错误,但我的问题却是一个更奇怪的问题,因为代码昨天(今天)运行良好,但今天没有运行(并且我没有更改任何代码).我的文件夹目录非常简单:

This seems to be a common error with file paths but my problem is a but more strange because code worked fine yesterday but not today (and I did not change any code). My folder directory is quite simple:

-node_modules
-public
    -css
    -js
    control_panel.html
    index.html
app.js
packages.json

我正在app.js中使用Express中间件来帮助呈现文件.

and I am using an Express middleware inside app.js to help render files.

var express = require("express");
var app = express();

app.use(express.static("public"));

app.get('/', function get(req, res) {
    res.sendFile('/index.html');
});

app.get('/control_panel', function get(req, res) {
    res.sendFile('/control_panel.html');
});

当我尝试在浏览器中打开index.html时,没有问题,一切正常.但是,当我尝试在浏览器中打开control_panel.html时,得到Error: ENOENT: no such file or directory, stat '/control_panel.html' at Error (native)

When I try to open index.html in the browser, there is no problem, everything works as expected. When I try to open control_panel.html in the browser, however, I get Error: ENOENT: no such file or directory, stat '/control_panel.html' at Error (native)

是什么原因引起的问题?

What is causing the problem?

推荐答案

基于您的情况的一些相关要点:

A number of relevant points based on your situation:

  1. 所有静态资产(html文件,图像,CSS文件,客户端脚本文件等)应使用适当的express.static(...)语句自动提供服务.您不应该为静态资源创建单独的路由.

  1. All static assets (html files, images, CSS files, client-side script files, etc...) should be serviced automatically with an appropriate express.static(...) statement. You should not be creating individual routes for static resources.

要使express.static()正常运行,必须在目录层次结构中找到所有静态资源,该目录层次结构仅包含旨在公开的文件.

To make express.static() work properly, you must locate all your static resources in a directory hierarchy that contains only files meant to be public.

您的私有服务器端文件(例如app.js)不应位于该公共目录层次结构中.他们应该在其他地方.

Your private server-side files such as app.js should not be in that public directory hierarchy. They should be elsewhere.

您到express.static()的路径不正确.

您的res.sendFile()路径不正确.

我建议您采取以下措施:

What I would suggest you do to fix things is the following:

  1. 将app.js移出public目录.它必须在私有目录中.我建议public目录是app.js所在的子目录.
  2. 然后,您在app.js中的express.static()将可以工作以提供静态HTML字段.
  3. 然后,您可以删除index.htmlcontrol_panel.html的两条路线,因为express.static()应该为它们服务.
  1. Move app.js out of the public directory. It needs to be in a private directory. I'd suggest the public directory be a sub-directory from where app.js is located.
  2. Then, your express.static() in app.js will work property to serve your static HTML fiels.
  3. Then, you can remove the two routes you have for index.html and control_panel.html because express.static() should be serving them.

这是一个可行的层次结构:

Here's one hierarchy that would work:

server
    app.js
    public
        index.html
        control_panel.html

然后是一个像这样的app.js:

And, an app.js like this:

var express = require("express");
var app = express();

// serve static files found in the public sub-directory automatically
app.use(express.static("public")); 

app.listen(80);

这篇关于ENOENT:没有这样的文件或目录,Express Middleware的stat错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆