How Use Node Js as Web Server Explain With Example?

//

Heather Bennett

Node.js is a powerful runtime environment that allows us to run JavaScript on the server-side, making it possible to build scalable and high-performance web applications. One of the key features of Node.js is its ability to function as a web server, handling incoming HTTP requests and serving responses. In this tutorial, we will explore how to use Node.js as a web server with examples.

Setting Up Node.js

Before we dive into using Node.js as a web server, we need to make sure it is installed on our system. Visit the official Node.js website and download the installer for your operating system. Once downloaded, run the installer and follow the prompts to complete the installation process.

Creating a Basic Web Server

To create a basic web server using Node.js, we start by creating a new directory for our project. Open your preferred code editor and create a new file called server.js.

Step 1: Import Required Modules


// Importing required modules
const http = require('http');

Step 2: Create the Server


// Creating the server
const server = http.createServer((req, res) => {
  // Handling incoming requests
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!');
});

Step 3: Start Listening for Requests


// Start listening for requests
server.listen(3000, 'localhost', () => {
  console.log('Server running at http://localhost:3000/');
});

Save the file and open your terminal or command prompt. Navigate to the project directory and run the following command to start the server:


node server.js

Once the server is running, open your web browser and visit http://localhost:3000/. You should see the message “Hello World!” displayed on the page.

Handling Different Routes

In a real-world web application, we often need to handle different routes or URLs. Node.js provides an easy way to achieve this.

Step 1: Update Server Code


// Creating the server
const server = http.createServer((req, res) => {
  // Handling different routes
  if (req.url === '/') {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Home Page');
  } else if (req.url === '/about') {
    res.end('About Page');
  } else {
    res.statusCode = 404;
    res.end('Page Not Found');
  }
});

Step 2: Restart the Server


node server.js

Now, if you visit http://localhost:3000/, you will see “Home Page” displayed. If you visit http://localhost:3000/about, you will see “About Page” displayed. For any other route, you will see “Page Not Found”.

Conclusion

Congratulations! You have learned how to use Node.js as a web server.

We covered the basics of creating a server, handling different routes, and serving responses.js provides great flexibility and scalability when it comes to building web applications. Now you can start exploring more advanced features and build powerful web servers using Node.js.

Remember to experiment and play around with the code examples provided in this tutorial. This will help solidify your understanding of using Node.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy