Ok, I'm new to Node.js so excuse the noob question. Heres my simple code:
var http = require('http');
var fs = require('fs');
// simple node.js server
server = http.createServer(function (request, response){
var get_text = function (errors, contents){
response.write(contents);
response.end('hello world');
}
// get_text is the callback
fs.readFile('log.txt', get_text);
response.write('End of the callback');
response.writeHead(200);
});
server.listen(8000);
console.log('Server running at port 8000');
As it stands, when I run the script in my terminal it will start the server properly but when I go to my localhost:8000 in my browser (chrome) it comes up as "webpage not available".
If I comment out the writeHead command it works fine.
Why?
This is because you're attempting to .writeHead() after .write():
response.write('End of the callback');
response.writeHead(200);
.writeHead() has to be first:
response.writeHead(200);
response.write('End of the callback\n');