错误:ENOENT:没有这样的文件或目录,错误(本机)中的 stat '/public/main.html' [英] Error: ENOENT: no such file or directory, stat '/public/main.html' at Error (native)
本文介绍了错误:ENOENT:没有这样的文件或目录,错误(本机)中的 stat '/public/main.html'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的 server.js
文件:
var express = require('express'),应用程序 = 快递();应用程序.use(express.static('./public')).get('*',function (req,res) {res.sendfile('/public/main.html');}).听(3000);
这是我的main.html
:
<头><title>联系人</title><base href'/'>头部><身体><div class="容器"><div class="page-header"><h1>联系人</h1>
</html>
和文件夹结构:
解决方案
由于服务器和索引文件都在public"目录中,您可以简单地使用:
res.sendfile('./main.html');
回答评论中的问题:在 Express 4.x 中,sendfile
方法被替换为 sendFile
方法(全部小写 -> 驼峰式).可能只是早期版本的疏忽,在后者中得到了修复.
This is my server.js
file:
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
:
<!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). Probably just an oversight in early versions, that got fixed in the latter.
这篇关于错误:ENOENT:没有这样的文件或目录,错误(本机)中的 stat '/public/main.html'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文