【发布时间】:2020-06-16 04:06:42
【问题描述】:
我正在尝试显示一个创建节点 http 服务器的 html 页面,当我尝试获取它返回未定义的 html 文件的代码时,这就是代码...
var http = require('http');
var fileContent = function(path, format) {
var fs = require('fs');
fs.readFile(path, format, function(error, contents) {
if (error) throw error;
return contents;
});
}
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
page = fileContent('./page.html','utf8');
console.log(page);
res.end(page);
}).listen(8080);
我打印了错误,
[Error: ENOENT: no such file or directory, open './page.html'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: './page.html'
}
两个文件在同一个目录
【问题讨论】:
-
尝试使用绝对 URL,我不确定
fs模块是否接受相对路径作为有效参数 -
你能显示你的文件夹结构吗
-
绝对路径也不行
-
文件夹 > index.js 和 page.html
-
虽然我不认为你只是给出相对路径有什么问题,仅供参考stackoverflow.com/questions/33342984/…