1.

Can we load a HTML code  into Node.Js?

Answer»

We can easily load HTML code into Node.Jsby making a change in the content TYPE.  In HTML Code content type is defined as “TEXT/plain” and for LOADING this into Node.Js we have to change it to “text/html”. PLEASE see below example for more detail:-  

fs.readFile(FILENAME, "binary", function(err, file) {  if(err) {   response.writeHead(500, {"Content-Type": "text/plain"});  response.write(err + "\n");  response.end();  return;  }  response.writeHead(200);  response.write(file, "binary");  response.end();  });

Now we have to modify the above code to load an HTML page instead of plain text like as below:- 

fs.readFile(filename, "binary", function(err, file) {  if(err) {   response.writeHead(500, {"Content-Type": "text/html"});  response.write(err + "\n");  response.end();  return;  }  response.writeHead(200, {"Content-Type": "text/html"});  response.write(file);  response.end();  });


Discussion

No Comment Found