0

I am new in NodeJS, I want to create a simple http server, Just send a 'Hello World' back to the client. I did something. If anyone, can check out my code if I did it right, if not add yours, I will be really appreciated.

Here is my code.

var http = require("http"); 
var server = http.createServer(function(request, response) {

response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html>"); 
response.write("<head>"); 
response.write("<title>Hello World!"); 
response.write("</head>"); 
response.write("<body>"); 
response.write("Hello World!"); 
response.write("</body>"); 
response.write("</html>"); 
response.end(); 
 });
 server.listen(80); 
 console.log("The Server is listening now..."); 

1 Answer 1

1

Your node.js is fine, however, you are missing a closing title tag in your html, eating up the rest of your page. Modify your title line so it is like so:

response.write("<title>Hello World!</title>"); 

and you should see output on the browser.

Happy Programming! :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.