Ich bin dran, diese Datei in Nodejs mit FS-Modul zu lesen. Ich bekomme die Antwort zweimal. lass mich wissen, was ich falsch mache. Hier ist mein Code.Wie liest man Dateien mit fs in der Knoten-App?
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);
Nach dem Aufrufen des Ports im Browser. Ich bekomme die Antwort zweimal in meinem Terminal. Hier ist die Ausgabe.
The end
this is sample text for the testing.
The end
this is sample text for the testing.
Der Code wird nicht funktionieren folgende tun. Und das Ergebnis passt nicht dazu. –
sample.txt enthält "Dies ist Beispieltext für das Testen." –