使用文件系统读取HTML文件

时间:2020-09-19 15:50:35

标签: javascript html node.js filesystems

此处{'Content-Type':'text / html'}的用途是什么?

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('demofile.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(8080);

console.log("Listening at 8080...");`

2 个答案:

答案 0 :(得分:-1)

答案 1 :(得分:-1)

当您要将数据发送回客户端时,可以将标头发送到该客户端。 'Content-Type': 'text/html与这段代码的客户端将找出它将接收html文件。 您可以将json文件或html文件发送回clinet。 'Content-Type': 'text/html ===>这将显示您正在发送html输出 'Content-Type': 'application json ====>这将显示您正在发送json输出