[英]Error: ENOENT: no such file or directory, stat '/public/main.html' at Error (native)
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:和文件夹结构:
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).回答评论中的问题:在 Express 4.x 中, sendfile
方法被sendFile
方法替换(全部小写 -> 驼峰式)。 Probably just an oversight in early versions, that got fixed in the latter.可能只是早期版本的疏忽,在后者中得到了修复。
For me using the "."对我来说使用“。” in the path didn't work, instead, I tweaked it as:在路径中不起作用,相反,我将其调整为:
res.sendFile(__dirname + '/public/main.html');
I had a similar issue when I referred to dist
folder.当我提到dist
文件夹时,我遇到了类似的问题。 the relative path to index.html was: index.html 的相对路径是:
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/project-name/index.html'));
});
You missed the dot .你错过了点。 Keep in mind relative directory is请记住相对目录是
res.sendfile('./public/main.html');
res.sendfile('/public/main.html');
应该改为
res.sendfile('./public/main.html');
这个解决方案对我有用:
res.sendfile('./main.html');
Just use this : res.sendFile(__dirname + '/main.html');
只需使用这个: res.sendFile(__dirname + '/main.html');
It'll definitely work.它肯定会起作用。 :) :)
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);
I had the same problem.我有同样的问题。 It appears that my client code was somehow not "in sync" with my server code (I'm not too familiar with the inner workings of node/express/angular and how they affect each other so "in sync" is the best I can do).看来我的客户端代码在某种程度上与我的服务器代码“不同步”(我不太熟悉 node/express/angular 的内部工作原理以及它们如何相互影响,所以“同步”是我能做到的最好的做)。 I had only rebuilt my server code (after having added one nodejs library and updated another, among other server code changes) with this:我只重建了我的服务器代码(在添加了一个 nodejs 库并更新了另一个,以及其他服务器代码更改之后):
npm run build.server
(for my project this only builds server side. I run this sometimes when I'm testing server changes because rebuilding the client code as well, without a watch, can take a long time) npm run build.server
(对于我的项目,这只构建服务器端。我有时在测试服务器更改时运行它,因为重建客户端代码,没有监视,可能需要很长时间)
Once I rebuilt the client code this issue was resolved:一旦我重建了客户端代码,这个问题就解决了:
npm run build -- dev
(for my project this builds everything, server and client code) npm run build -- dev
(对于我的项目,这会构建所有内容,服务器和客户端代码)
__dirname has been deprecated and the suggested solutions may not work currently (August 2021). __dirname 已被弃用,建议的解决方案目前可能不起作用(2021 年 8 月)。 In Express 4.x this has been simplified and you wouldn't need to use 'path' module, you just have to specify a root option like this:在 Express 4.x 中,这已经被简化,你不需要使用 'path' 模块,你只需要指定一个像这样的 root 选项:
app.get('*', (req, res) => {
res.sendFile('main.html', {root: 'public'});
});
I had the same problem.我有同样的问题。 After emailing heroku, mine was a case sensitivity problem.在给 heroku 发电子邮件后,我的是一个区分大小写的问题。 One of my files was in all caps, and i had to make adjustments from there.我的一个文件全部大写,我不得不从那里进行调整。
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.我有一个正在更改当前目录的服务器端 shell 脚本。 Since we use relative paths in sendfile
I started seeing this error after this script would run.由于我们在sendfile
使用相对路径,因此在此脚本运行后我开始看到此错误。 This shell script was being run by Node.这个 shell 脚本是由 Node.js 运行的。
For my case I used nestjs with ServeStaticModule and it helps to add serveRoot option :对于我的情况,我使用了带有 ServeStaticModule 的nestjs ,它有助于添加 serveRoot 选项:
ServeStaticModule.forRoot({
// play with options below
rootPath: path.resolve(`./src/assets`), // I prefer to use resolve in your case it can be better to use path.join
serveRoot: '/assets',
})
So then I found files on: http://localhost:3000/assets/1.png然后我在以下位置找到了文件:http://localhost:3000/assets/1.png
Other options: ServeStaticModuleOptions其他选项: ServeStaticModuleOptions
my server.js我的 server.js
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/dist/projectName'));
app.get('/*', function(req, res){
res.sendFile(__dirname + '/dist/projectName/index.html');
});
app.listen(4200);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.