我正在使用fs模块在nodejs中读取此文件。 我收到了两次回复。让我知道我做错了什么。这是我的代码。如何在节点应用程序中使用fs读取文件?
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包含“这是测试的示例文本”。 –