【发布时间】:2020-10-26 10:55:47
【问题描述】:
我试图从我的节点应用程序中显示一个 html 文件。这是我的代码:
var http = require('http');
var fs = require ('fs');
var path = require("path");
var filePath = path.normalize('/NodeJS/projects/test.html');
var server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type' : 'text/html'});
var myReadStream = fs.createReadStream(filePath);
myReadStream.pipe(res);
});
server.listen(3000, '127.0.0.1');
console.log('listening to port 3000');
我之前在尝试使用 __dirname 时遇到了同样的问题,但它不起作用,所以我想尝试使用 path.normalize。 关于为什么这不起作用的任何指针。如果我将控制台错误中的目录复制到我的资源管理器中,我的 test.html 文件将打开....该文件就在那里。这一定是一些简单的错误,但它杀死了我
【问题讨论】:
-
我有几乎同样的问题。但是对于作为 Windows 服务安装的我来说,它无法加载文件。但通常使用 node server.js 它可以工作。
标签: javascript html node.js path