This variant is better because you could not know whether file exists or not. You should send correct header when you know for certain that you can read contents of your file. Also, if you have branches of code that does not finish with '.end()', browser will wait until it get them. In other words, your browser will wait a long time.
var fs = require("fs");
var filename = "./index.html";
function start(resp) {
fs.readFile(filename, "utf8", function(err, data) {
if (err) {
// may be filename does not exists?
resp.writeHead(404, {
'Content-Type' : 'text/html'
});
// log this error into browser
resp.write(err.toString());
resp.end();
} else {
resp.writeHead(200, {
"Content-Type": "text/html"
});
resp.write(data.toString());
resp.end();
}
});
}