Javascript 错误:ENOENT:没有这样的文件或目录,错误(本机)中的 stat '/public/main.html'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36340747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: ENOENT: no such file or directory, stat '/public/main.html' at Error (native)
提问by pradeep murugan
This is my server.js
file:
这是我的server.js
文件:
var express = require('express'),
app = express();
app
.use(express.static('./public'))
.get('*',function (req,res) {
res.sendfile('/public/main.html');
})
.listen(3000);
This is my main.html
:
这是我的main.html
:
<!DOCTYPE html>
<html>
<head>
<titel>Contacts</titel>
<base href'/'>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Contatcs</h1>
</div>
</div>
</body>
</html>
And the folder structure:
和文件夹结构:
采纳答案by Bernard Saucier
Since both the server and the index file are INSIDE the "public" directory, you can simply use :
由于服务器和索引文件都在“公共”目录内,您可以简单地使用:
res.sendfile('./main.html');
To answer the question in the comments : In Express 4.x, the sendfile
method was replaced by the sendFile
method (all lowercase -> camelCase). Probably just an oversight in early versions, that got fixed in the latter.
回答评论中的问题:在 Express 4.x 中,sendfile
方法被替换为sendFile
方法(全部小写 -> 驼峰式)。可能只是早期版本的疏忽,在后者中得到了修复。
回答by akshaymittal143
I had a similar issue when I referred to dist
folder.
the relative path to index.html was:
当我提到dist
文件夹时,我遇到了类似的问题。index.html 的相对路径是:
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/project-name/index.html'));
});
回答by Niloo
This solution works for me:
这个解决方案对我有用:
res.sendfile('./main.html');
回答by Shubham joshi
For me using the "." in the path didn't work, instead, I tweaked it as:
对我来说使用“。” 在路径中不起作用,相反,我将其调整为:
res.sendFile(__dirname + '/public/main.html');
回答by vkstack
You missed the dot. Keep in mind relative directory is
你错过了点。请记住相对目录是
res.sendfile('./public/main.html');
回答by Guy Korland
res.sendfile('/public/main.html');
should be changed to
应该改为
res.sendfile('./public/main.html');
回答by pbhadu21
Just use this : res.sendFile(__dirname + '/main.html');
It'll definitely work. :)
只需使用这个:res.sendFile(__dirname + '/main.html');
它肯定会起作用。:)
回答by pranav rai
server.js
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/dist/projectName'));
app.post('/*', function(req, res){
res.sendFile(__dirname + '/dist/projectName/index.html');
});
app.listen(4200);
回答by jared thomas
I had the same problem. After emailing heroku, mine was a case sensitivity problem. One of my files was in all caps, and i had to make adjustments from there.
我有同样的问题。在给 heroku 发电子邮件后,我的是一个区分大小写的问题。我的一个文件全部大写,我不得不从那里进行调整。
回答by Santosh
Though OP's cause is different for this error I got the same error due to a different reason so I am posting this for other's who come here.
尽管 OP 的原因因此错误而异,但由于不同的原因,我遇到了相同的错误,因此我将其发布给其他来这里的人。
I had a server-side shell script that was changing the current directory. Since we use relative paths in sendfile
I started seeing this error after this script would run. This shell script was being run by Node.
我有一个正在更改当前目录的服务器端 shell 脚本。由于我们sendfile
在此脚本运行后使用相对路径,因此我开始看到此错误。这个 shell 脚本是由 Node.js 运行的。