CREATE A SIMPLE WEBSERVER IN NODEJS

    

     


What is a Web Server ……….?

It is a machine that stores web information and answers to client requests made through the internet. These are also in charge of running several websites from remote locations. When a request is made, these web servers deliver the webpages to the users, and a webpage is a collection of content, photos, videos, and so on that is hosted on a web server. These exchanges will take place through protocols, most notably HTTP (Hyper Text Transfer Protocol) and HTTPS (Highly Secure HTTP).

Prerequisites for beginning with a basic Nodejs program

  1. For first practice on this notion, tools like Notepad / Notepad ++ are sufficient, but in the future, while developing large apps, we will need to use tools like Visual Studio Code.

  2. To run a Nodejs program on your system, you must have Nodejs installed, together with the necessary parameters and system settings. To verify the installation,

  • è Open command prompt (cmd), type node -v

Creation of a simple Web Server in Nodejs

To start with, open either of your notepad / vscode, and write the below code



Explanation of the webserver code:-

  • 1st line -> The first line is an import statement that consumes the attributes of the http protocol in a variable called http.
  • 3rd line -> We are building a web server using the http protocol and assigning it to a server variable using the createServer function. This technique takes a function as a parameter.

        We can initialize the functions in a variety of methods, some of which are as follows:

    i)                 Arrow function = (req, res) => {}

    ii)                ii) Using the function keyword -> function(req, res) {}

        (req, res) are the function parameters, the req is used to request information from the server and             the res is used to deliver the response to the website.

  • 4th line -> Using the res.write method, the message "A Simple Nodejs Programme" is written to the webpage.
  • 5th line -> It concludes the response procedure using res.end()
  • 7th line -> We are assigning a PORT number to the server in order for it to handle requests and responses. We've set the value to 3000, but you can experiment with different port numbers.
  • 8th line -> This is a log message that is often displayed in the terminal when we run the aforementioned webserver program.  

Execution of the above program:-

  1.   First save the above code with a file name and .js extension at the end, in a particular location
  2.  Open cmd, and navigate to the above saved file location
  3. Write node filename.js and then click ENTER, in cmd.

  • The above message clearly indicates, our web server program is running on the PORT number 3000, so open any browser (chrome / Internet Explorer, etc.,) and hit this url to see the response on the webpage (http://localhost:3000)


Harsha Vardhan Garlapati

As a software engineer and technology enthusiast, I thrive on exploring the ever-evolving world of technology and leveraging it to create innovative solutions. With a passion for sharing knowledge and insights, I also delve into the world of blogging to inspire and educate others in the realm of software development. Beyond the realm of code, I find joy in exploring new destinations as a passionate traveler, embracing diverse cultures, and broadening my horizons. In my free time, I cherish moments spent with friends, valuing the connections and experiences that enrich my life. Combining my technical expertise, love for writing, adventurous spirit, and the importance of human connections, I aim to leave a positive impact on the world through my work, experiences, and interactions. Let’s embark on this exciting journey together, exploring the wonders of technology while cherishing the moments that truly matter.

Post a Comment

Previous Post Next Post