【问题标题】:Using File Systems to read Html files [duplicate]使用文件系统读取 Html 文件 [重复]
【发布时间】:2021-01-06 06:29:45
【问题描述】:

这里的 {'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...");`

【问题讨论】:

  • 这能回答你的问题吗? Is it necessary to set a Content-Type in Node.js?
  • 请在提出问题之前尝试至少做一个非常少的研究(只需在 Google 搜索框中输入“什么是内容类型标题?”问题并按 Enter 键)。这将帮助您在未来提出更好的问题。

标签: javascript html node.js filesystems


【解决方案1】:

【讨论】:

  • 请为任何链接添加上下文,以便您的答案是独立的,这意味着答案需要在答案本身中。见"Provide context for links"
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2018-11-07
    • 2016-09-27
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多