오류: ENOENT: 해당 파일 또는 디렉터리가 없습니다. 오류(네이티브)에서 '/public/main.html'을 stat합니다.

이건 내 거야server.js파일:
var express = require('express'),
app = express();
app
.use(express.static('./public'))
.get('*',function (req,res) {
res.sendfile('/public/main.html');
})
.listen(3000);
이건 내 거야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>
폴더 구조는 다음과 같습니다.

서버와 인덱스 파일은 모두 「퍼블릭」디렉토리내에 있기 때문에, 간단하게 사용할 수 있습니다.
res.sendfile('./main.html');
댓글로 질문에 답하려면: Express 4.x에서는sendfile메서드가 로 대체되었습니다.sendFilemethod(모두 소문자 -> camel Case).아마 초기 버전에서는 실수를 했겠지만, 후자 버전에서는 고쳤겠죠.
__dirname은 더 이상 사용되지 않으며 제안된 솔루션이 현재(2021년 8월) 작동하지 않을 수 있습니다.Express 4.x에서는 이 기능이 간소화되어 'path' 모듈을 사용할 필요가 없습니다.루트 옵션을 다음과 같이 지정하기만 하면 됩니다.
app.get('*', (req, res) => {
res.sendFile('main.html', {root: 'public'});
});
경로에서 ""를 사용하는 것은 작동하지 않았지만 다음과 같이 수정했습니다.
res.sendFile(__dirname + '/public/main.html');
넌 요점을 놓쳤어.상대 디렉토리는
res.sendfile('./public/main.html');
저도 비슷한 문제가 있었어요dist폴더입니다.index.interface의 상대 경로는 다음과 같습니다.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/project-name/index.html'));
});
이 솔루션은 다음과 같이 기능합니다.
res.sendfile('./main.html');
res.sendfile('/public/main.html');
로 변경되어야 한다
res.sendfile('./public/main.html');
이것만 사용해 주세요.res.sendFile(__dirname + '/main.html');분명 효과가 있을 거예요.:)
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);
저도 같은 문제가 있었어요.클라이언트 코드가 서버 코드와 '동기화'되지 않은 것 같습니다(노드/익스프레스/각도의 내부 동작과 서로 어떻게 영향을 미치는지 잘 모르기 때문에 '동기화'가 최선입니다).서버 코드를 재구축한 것은 다음과 같습니다(다른 서버 코드 변경 중 1개의 nodejs 라이브러리를 추가하고 다른 라이브러리를 업데이트한 후).
npm run build.server(프로젝트에서는 서버측만 구축합니다.클라이언트 코드도 워치 없이 재구축하는 데 시간이 오래 걸릴 수 있기 때문에 서버 변경을 테스트할 때 이 작업을 실행하는 경우가 있습니다.)
클라이언트 코드를 재구축하면 이 문제는 해결되었습니다.
npm run build -- dev(프로젝트에서는 모든 것, 서버 및 클라이언트 코드가 구축됩니다.)
저도 같은 문제가 있었어요.heroku에게 메일을 보낸 후, 저는 대소문자를 구분하는 문제가 되었습니다.내 파일 중 하나가 모두 대문자로 되어 있어서 거기서부터 수정해야 했어.
이번 에러는 OP의 원인은 다르지만, 다른 이유로 같은 에러가 발생했기 때문에, 여기에 와주신 다른 분들을 위해 글을 올립니다.
현재 디렉토리를 변경하는 서버 측 셸 스크립트가 있었습니다.에서는 상대 패스를 사용하고 있기 때문에sendfile이 스크립트가 실행된 후 이 오류가 표시되기 시작했습니다.이 셸 스크립트는 노드에서 실행되고 있었습니다.
제 경우 serveStatic Module에서 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',
})
그 후 http://localhost:3000/assets/1.png에서 파일을 찾았습니다.
기타 옵션:ServeStaticModuleOptions
my server.displays
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);
언급URL : https://stackoverflow.com/questions/36340747/error-enoent-no-such-file-or-directory-stat-public-main-html-at-error-nat
'programing' 카테고리의 다른 글
| 기능 상태 비저장 구성 요소의 PropTypes (0) | 2023.03.06 |
|---|---|
| React 구성 요소가 기본 내보내기로 없으면 React/Next 프로젝트 검색 페이지를 빌드할 수 없습니다(콘텍스트 api 파일). (0) | 2023.03.06 |
| WordPress 플러그인을 디버깅하려면 어떻게 해야 합니까? (0) | 2023.03.06 |
| 이스케이프된 JSON 문자열을 마킹 해제하는 방법 (0) | 2023.03.06 |
| YAML을 사용한 Spring @PropertySource (0) | 2023.03.06 |