Javascript 什么';下面的公用目录设置有什么问题?

Javascript 什么';下面的公用目录设置有什么问题?,javascript,node.js,express,Javascript,Node.js,Express,我刚刚创建了一个网页包项目: static/ // where I have the js and css file index.html // the main file 并将其放置在Express设置中的public/文件夹中: var express = require('express'); var app = express(); app.get('/', function (req, res) { res.sendfile('public/index.html') });

我刚刚创建了一个网页包项目:

static/ // where I have the js and css file
index.html // the main file
并将其放置在Express设置中的
public/
文件夹中:

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

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

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});
但我有以下错误:

http://localhost:3000/static/app.79c874fce37c5d0a32117271eb04a7f8.css 
http://localhost:3000/static/app.57fd4b2335b940c7b8d1.js 404 (Not Found)
这是
index.html
的内容:

<!DOCTYPE html>
<html>

<head>
  <meta charset=utf-8>
  <title>istaging-viewer</title>
  <link href=/static/app.79c874fce37c5d0a32117271eb04a7f8.css rel=stylesheet>
</head>

<body>
  <div id=app>
    <script src=/static/app.57fd4b2335b940c7b8d1.js></script>
  </div>
</body>

</html>

。。。但我仍然得到这个错误:

GET http://localhost:3000/static/app.79c874fce37c5d0a32117271eb04a7f8.css 
localhost/:1 GET http://localhost:3000/static/app.57fd4b2335b940c7b8d1.js 404 (Not Found)
编辑2:

我尝试了以下方法来提供静态文件:

app.use(express.static('static'));
我仍然会遇到同样的错误:

GET http://localhost:3000/static/app.79c874fce37c5d0a32117271eb04a7f8.css 
localhost/:1 GET http://localhost:3000/static/app.57fd4b2335b940c7b8d1.js 404 (Not Found)

只要你的app/main js文件和index.html在同一个文件夹中,那么

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

但是,理想情况下,您的文件结构应该如下所示:

app.js
static/
    index.html
    js/
    css/
    img/
这样,你就可以这样引用它

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

只要你的app/main js文件和index.html在同一个文件夹中,那么

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

但是,理想情况下,您的文件结构应该如下所示:

app.js
static/
    index.html
    js/
    css/
    img/
这样,你就可以这样引用它

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

谢谢你的帮助。我得到这个:
Error:enoint:no这样的文件或目录,stat'E:\alex\express test\static\index.html'at Error(native)
我必须删除
public
文件夹,也许?我得到了与新代码相同的错误:
Error:enoint:no这样的文件或目录,stat'E:\alex\express test\index.html'at Error(native)
我按照你的建议做了。请查看我的编辑。谢谢你的帮助。我得到这个:
Error:enoint:no这样的文件或目录,stat'E:\alex\express test\static\index.html'at Error(native)
我必须删除
public
文件夹,也许?我得到了与新代码相同的错误:
Error:enoint:no这样的文件或目录,stat'E:\alex\express test\index.html'at Error(native)
我按照您的建议做了。请查看我的编辑。您正在通过express处理index.html文件,但不是静态资产,express需要了解这些资产。请参阅本页:。第一行代码是对项目目录结构的直接复制和粘贴。@martinczerwi我尝试了你的建议。请查看我的编辑2。请尝试此
应用程序。使用(express.static('public')@martinczerwi不起作用。。。因为我现在使用的是编辑结构,所以我没有公用文件夹了。啊,对不起。然后需要更改索引文件,因为静态文件夹是请求的根。您需要引用文件,如
localhost:3000/app.87d…123.css
(刚刚编辑,我忘记了端口号;)您正在通过express处理index.html文件,但不是静态资产,而express需要知道静态资产。请参阅本页:。第一行代码是对项目目录结构的直接复制和粘贴。@martinczerwi我尝试了你的建议。请查看我的编辑2。请尝试此
应用程序。使用(express.static('public')@martinczerwi不起作用。。。因为我现在使用的是编辑结构,所以我没有公用文件夹了。啊,对不起。然后需要更改索引文件,因为静态文件夹是请求的根。您需要引用像
localhost:3000/app.87d…123.css这样的文件(刚刚编辑,我忘记了端口号;)