Node Js - Create server using the HTTP module
To create a server in Node we are going to use the HTTP module in Node that will be handling all requests to your application.
Open localhost:8000 in the browser
Note: You can change the port on the server.listen() function.
Code for the reference :
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</title>");
response.write("</head>");
response.write("<body>");
response.write("<b>Hello World!</b>");
response.write("</body>");
response.write("</html>");
response.end();
});
server.listen(8000);
console.log("Node Server is listening");
The above code is a very basic Node js server returning an Html with the "Hello World" body when we try to reach the port of the server.
0 Comments