【发布时间】:2017-10-28 21:58:56
【问题描述】:
我正在尝试使用 fs 模块在 nodejs 中读取此文件。 我得到了两次回应。让我知道我做错了什么。这是我的代码。
var http = require("http");
var fs = require("fs");
http.createServer(function(req, res) {
fs.readFile('sample.txt', function(err, sampleData) {
console.log(String(sampleData));
//res.end();
});
console.log("The end");
// res.writeHead(200);
res.end();
}).listen(2000);
在浏览器中点击端口后。我在终端中收到了两次回复。这是输出。
The end
this is sample text for the testing.
The end
this is sample text for the testing.
【问题讨论】:
-
代码不起作用。结果不适合它。
-
sample.txt 包含“这是用于测试的示例文本。”
标签: javascript node.js http fs