HTTP Module in Node.js

Node.js is great for making websites and online tools because it’s good at handling lots of tasks at once, works quickly, and can be easily set up. It has a special feature called the HTTP module that lets you make web servers and handle web traffic. You don’t need to add anything extra to use it; it’s already part of Node.js. In this guide, we’ll show you how to use this feature to make your own web applications, starting with the basics and moving on to more advanced stuff.

The Built-in HTTP Module

The HTTP module lets you create your own web server that can listen for and respond to requests from users. To start using it, you first need to add it to your Node.js project like this:

const http = require('http');

Once you’ve done that, you can set up a basic server that sends a simple message to anyone who visits it. Here’s an example:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');

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

This code makes a server that says “Hello World” whenever someone connects to it.

Step-1: Node.js as a Web Server

With the HTTP module, you can turn Node.js into a web server. This means it can show web pages, handle user input, and more. The example we showed is just the beginning. You can make your server do a lot more, like dealing with different parts of a website and working with user requests.

Step-2: Add an HTTP Header

HTTP headers are important because they tell the web browser and server how to handle the web page or data being sent. For instance, you can tell the browser to expect a web page by setting the content type like this:

res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Hello, World!</h1>');

This code tells the browser that the response is a web page, which includes a simple greeting.

Step-3: Read the Query String

A query string is a part of a web address that can send data to the server. You can read this data in Node.js by using another feature called the URL module. Here’s how:

const http = require('http');
const url = require('url');

http.createServer((req, res) => {
  const queryObject = url.parse(req.url, true).query;
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Received parameters: ' + JSON.stringify(queryObject));

If you visit http://localhost:3000/?name=John&age=30, the server will show you the data you sent.

Step-4: Split the Query String

When you use data from a website address, you have to break it into smaller pieces. The URL part of Node.js helps you by converting website address data into easy-to-use objects. The example talked about shows you how to select pieces of data from a website address.

This is just the beginning of what you can do with Node.js’ Internet Tools. There is a lot to learn, such as how to send files, how to manage uploads and how to secure your website. As you look at it, you’ll see that Node.js is a powerful tool for building Internet applications. It gives you everything you need to work with web pages, user information and more, all in one place. With what you’ve learned, you can start building your own online applications and discover more about what Node.js can do.