在nodejs中读取HTML文件内容时出现TypeError

时间:2017-09-12 19:39:12

标签: javascript html node.js

我正在尝试使用nodejs的fs模块读取HTML文件内容。但是,我得到了TypeError。

有人可以告诉我代码中出了什么问题吗?

HTML代码。

<html>
<body>
    <h1>My Header</h1>
    <p>My paragraph</p>
</body>
</html>

Nodejs代码

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
  //Open a file on the server and return it's content:
  fs.readFile('demofile1.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write("Hey");
    res.write(data);
    return res.end();
  });
}).listen(8080);

错误

  

_http_outgoing.js:457       抛出新的TypeError('第一个参数必须是字符串或缓冲区');       ^

     

TypeError:第一个参数必须是字符串或缓冲区       在ServerResponse.OutgoingMessage.write(_http_outgoing.js:457:11)       在ReadFileContext.callback(D:\ Java \ Nodejs \ public \ readfile.js:8:9)       在FSReqWrap.readFileAfterOpen [as oncomplete](fs.js:366:13)

0 个答案:

没有答案