I got some annoying error on very simple nodejs response.write from http module. response.write don't output parameter. Help me please !
var server = http.createServer(function(request, response){
console.log("Connection");
var pathname = url.parse(request.url).pathname;
switch(pathname) {
case '/':
response.writeHead(200, {"Content-Type": "text/html"});
response.write("hello world");
break;
case '/socket.html':
fs.readFile(__dirname + pathname, function(error, data) {
if(error) {
response.writeHead(404);
response.write("Oops this doesn't exist - socket.html");
}else {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data, 'utf8');
}
});
break;
default :
response.writeHead(404);
response.write("Oops this doesn't exist - default");
break;
}
response.end();
Above codes block is apart of my server.js file.
you should use response.send(msg) instead of response.write() because response.write itself need to end with response.end(). So basically,
Response.send("something");
or
Response.write("something");
Response.end();
Each one'll be fine.