Create HTTP Web Server in Node.js
The Node.js framework is mostly used to create server based applications. The framework can easily be used to create web servers which can serve content to users.
There are a variety of modules such as the “http” and “request” module, which helps in processing server related requests in the web server space. We will have a look at how we can create a basic web server application using Node js.
1. Node as web server using HTTP
Our application is going to create a simple server module which will listen on port no 3000. If a request is made through the browser on this port no, then server application will send a ‘Hello’ World’ response to the client.
Example : server.js
//1. Calls the http library
var http = require('http');
//2. Create the serve using the http library
var server = http.createServer((function(request, response) {
//3. Set the content header
response.writeHead(200, { "Content-Type": "text/plain" });
//4. Send the string to the response
response.end("Hello World\n");
}));
//5. Make the server listen on port 3000
server.listen(3000,function(){
console.log('Listening port : 3000')
});
Save the above code in “server.js” and run the command “node server.js”
Output
Hellow World
here “http” is the built in module of Node JS through which we can transfer data over HTTP protocols. here is the list of all the built-in modules of Node JS
now to run your node server run the command in your terminal : “node server.js”
you will see “Listening port : 3000” message in your terminal. now go to http://localhost:3000/ you will see output in browser
2. if you want response displayed as HTML, include http header and start server
var http = require('http');
//2. Create the serve using the http library
var server = http.createServer((function(request, response) {
//3. Set the content header
response.writeHead(200, {'Content-Type': 'text/html'}); // http header
//4. Send the string to the response
response.write('<h1>Hello World!<h1>'); //write a response
response.end(); //end the response
}));
//5. Make the server listen on port 3000
server.listen(3000,function(){
console.log('Listening port : 3000')
});
if we browse any other localhost urls apart from http://localhost:3000/ it is also showing “hello world”. e.g: http://localhost:3000/about , http://localhost:3000/contact . NO.. it will not show “hello world”
3. let’s add some routing to our server.js
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'}); // http header
var url = req.url;
if(url ==='/about'){
res.write('<h1>about us page<h1>'); //write a response
res.end(); //end the response
}else if(url ==='/contact'){
res.write('<h1>contact us page<h1>'); //write a response
res.end(); //end the response
}else{
res.write('<h1>Hello World!<h1>'); //write a response
res.end(); //end the response
}
}).listen(3000, function(){
console.log("Listening Port : 3000"); //the server object listens on port 3000
});
Now again start server and check browser with other route
-
http://localhost:3000/
-
http://localhost:3000/about
-
http://localhost:3000/contact
It will display corresponding html content based on route (ex: about us page, contact us page, hello wolr)