![](/img/trans.png)
[英]How to read a text file with Node.js and write it to a HTML page?
[英]node.js does not read and return simple HTML file
我在node.js中創建了一個簡單的filereader方法,該方法在同一文件夾中接收一個html文件。 但是,當我嘗試在本地主機上啟動它時,我收到消息:throw new TypeError <'第一個參數必須是字符串或Buffer'>;
我的node.js文件是:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}).listen(8080);
我的HTML代碼也很簡單:
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
如果有人可以提供一些見識,將不勝感激。 我同時查看了Google和StackOverflow,但沒有解決方案解決我的問題。 其他節點程序(例如輸出“ Hello World”或顯示日期和時間)在我的本地主機上運行。
很有可能設置了err
並且undefined
data
。 您應該始終先檢查錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.