Node.js - Web Module
What is a Web Server?
A Web Server is a software application which handles HTTP requests sent by the HTTP client, like web browsers, and returns web pages in response to the clients. Web servers usually deliver html documents along with images, style sheets, and scripts.
Most of the web servers support server-side scripts, using scripting languages or redirecting the task to an application server which retrieves data from a database and performs complex logic and then sends a result to the HTTP client through the Web server.
Apache web server is one of the most commonly used web servers. It is an open source project.
But, node.js provides capabilities to create our own web server which will handle HTTP requests asynchronously. We can use IIS or Apache to run Node.js web application but it is recommended to use Node.js web server.
Web Application Architecture
A Web application is usually divided into four layers:
Client Layer: This layer consists of web browsers, mobile browsers or applications which can make HTTP requests to the web server.
Server Layer: This layer has the Web server which can intercept the requests made by the clients and pass them the response.
Business Layer: This layer contains the application server which is utilized by the web server to do the required processing. This layer interacts with the data layer via the database or some external programs.
Data Layer: This layer contains the databases or any other source of data.
Creating a Web Server using Node
Node.js makes it easy to create a simple web server that processes incoming requests asynchronously. It provides an http module which can be used to create an HTTP client of a server. Following is the bare minimum structure of the HTTP server which listens at 8012 port.
For this we have to create a js file and name it as server.js:
Next we have to create a html file named index.html in the same directory where we created server.js.
Now we will run the server.js to see the result −
Make a request to Node.js server
To make a http request we have to open any browser and write http://localhost:8012/index.html and we will see the following result.
If you have any queries regarding this blog or need any help you can contact us on: firstname.lastname@example.org