Answer the question
In order to leave comments, you need to log in
fs module in Node.Js, why did it return undefined?
Hello. I am a beginner in learning Node JS modules. I ran into a problem like this, first the code:
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res){
res.writeHead(200, {'Content-type':'text-html; charset=utf-8'});
res.write("Hello World ");
fs.readFile('index.html', function(err, data){
res.write(decodeURIComponent(data));
res.end();
});
}).listen(8080);
<hr><strong>Message</strong>
Answer the question
In order to leave comments, you need to log in
It's strange, but your code works fine for me ...
I would check for you what is in err, try to make the path to the file relative and generally see what is in the variables ...
maybe like this:
fs.readFile('./index.html', function(err, data){
console.log('readFile err', err);
console.log('readFile data', data);
console.log('readFile decData', decodeURIComponent(data));
res.write(decodeURIComponent(data));
res.end();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question