user7560350
user7560350

Reputation:

How to create a simple http server with NodeJS?

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..."); 

Upvotes: 0

Views: 284

Answers (1)

Josh
Josh

Reputation: 332

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! :)

Upvotes: 1

Related Questions