Loading

Quipoin Menu

Learn • Practice • Grow

node-js / Creating HTTP Server
tutorial

Creating HTTP Server

Imagine you're opening a restaurant. You need a building, a kitchen, waiters, and a menu. In the web world, an **HTTP server** is that building – it's the foundation that listens for customers (requests) and serves them food (responses). Node.js makes it incredibly easy to create your own HTTP server.

What is an HTTP Server?

An HTTP server is software that understands URLs and HTTP protocols. It listens for incoming requests from clients (like browsers) and sends back responses (like HTML pages, JSON data, or files).

Think of an HTTP server as a waiter in a restaurant. The waiter stands at the door, listens for customers, takes their order, goes to the kitchen, and brings back the food. Your Node.js server does exactly that for web requests.

The HTTP Module

Node.js has a built-in module called `http` that provides the functionality to create an HTTP server. You don't need to install anything – just require it.
const http = require('http');

Creating Your First Server

Here's the simplest HTTP server you can create:
const http = require('http');

<!-- Create a server -->
const server = http.createServer((req, res) => {
  <!-- This function runs for every request -->
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, World!n');
});

<!-- Start the server on port 3000 -->
server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Save this as `server.js` and run it:
node server.js

Open your browser and go to `http://localhost:3000`. You'll see "Hello, World!"

Understanding the Code

PartExplanation
`http.createServer(callback)`Creates a new HTTP server. The callback is called for every incoming request.
`req` (request)Contains information about the request (URL, headers, method).
`res` (response)Used to send a response back to the client.
`res.writeHead()`Sets the HTTP status code and headers.
`res.end()`Sends the response body and ends the response.
`server.listen(port, callback)`Starts the server and makes it listen on the specified port.

Adding More Functionality

You can send different types of responses:
server.on('request', (req, res) => {
  <!-- Send HTML -->
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end('

Hello World

'
);
});

Handling Different URLs

You can check the `req.url` property to send different responses for different paths:
const server = http.createServer((req, res) => {
  if (req.url === '/') {
    res.end('Home Page');
  } else if (req.url === '/about') {
    res.end('About Page');
  } else {
    res.writeHead(404);
    res.end('404 Not Found');
  }
});

Two Minute Drill

  • Use the `http` module to create servers.
  • `http.createServer()` takes a callback that receives `req` and `res`.
  • `res.writeHead()` sets status and headers.
  • `res.end()` sends the response body.
  • `server.listen()` starts the server on a port.
  • You can handle different URLs by checking `req.url`.

Need more clarification?

Drop us an email at career@quipoinfotech.com