DEV Community

Cover image for Node.js Request-Response Cycle Demystified with Examples - Part 8 Tutorial Series
Abdelhakim mohamed
Abdelhakim mohamed

Posted on

Node.js Request-Response Cycle Demystified with Examples - Part 8 Tutorial Series

Introduction:

The request-response model is the backbone of how the web works. It's the process that happens when you visit a website or call an API, with your browser (or client) asking for something and the server replying.


What is the Request-Response Pattern?

  • Client sends a request to the server (think of it as asking for information).
  • The server processes the request and sends back a response (this is the answer).

This pattern is what powers HTTP and makes the web tick.


Request-Response in Node.js:

Node.js handles this using a non-blocking, event-driven approach. This makes it fast and efficient even when dealing with many requests at once.

Here’s how it looks in code:

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/');
});
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The server listens for requests on port 3000.
  • It responds with "Hello, World!" when a request is made.

Key Components:

  1. Client: Your browser or app sending the request.
  2. Request: Contains info like method (GET, POST), URL, and sometimes data.
  3. Server: Where the request goes for processing.
  4. Response: What the server sends back (status code, data, etc.).

Image description


Common Request Types:

  • GET: Fetching data.
  • POST: Sending data to the server.
  • PUT: Updating existing data.
  • PATCH: Partially updating existing data.
  • DELETE: Removing data.

Example:

const server = http.createServer((req, res) => {
  if (req.method === 'GET') {
    res.end('You made a GET request');
  } else if (req.method === 'POST') {
    res.end('You made a POST request');
  }
});
Enter fullscreen mode Exit fullscreen mode

Using Express for Simplicity:

Express.js makes handling requests and responses even easier. Here’s an example:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Express handles the routing and simplifies server logic.

Advantages of the Request-Response Model:

  • Easy to understand.
  • Widely used: Works everywhere via HTTP.
  • Stateless: Each request stands alone (no memory of previous requests).

Conclusion

The request-response pattern is the heart of web communication! Whether you're building a simple web app or a complex API, understanding this model is essential. Try experimenting with Node.js or Express to see how this works in practice.


References:


Thank you for reading, and happy coding! 🎉

Top comments (0)