DEV Community

Shifa
Shifa

Posted on

Introduction to Server in Backend Development

What is Server?

Before reading the article think of server in your mind , Great did you just saw big hardware devices in front of you placed in a room?
Now let me tell you the Reality
In backend development, a server is not just a physical machine it’s primarily a software application that listens for requests from clients (such as browsers or mobile apps), processes them, and sends back responses. Servers act as the middle layer between the frontend (user interface) and backend logic or database.

Physical Server image

                              **VS**
Enter fullscreen mode Exit fullscreen mode

Cloud Server image

Using Node.js, we can create custom servers to handle data, control access, and manage communication between different parts of an application.

The Classic Server Setup (Browser-Based)
Before using tools like Postman or frameworks like Express.js, it's useful to understand the basic server setup in Node.js. In the classic approach, we create a server that listens on a specific port and returns a plain message (usually text or HTML) directly in the browser.

This type of server is helpful for understanding how the request-response cycle works at a low level. When you open a URL like http://localhost:3000, your browser sends a request to the server, which returns a message that gets displayed on the screen.

There’s no routing logic or data handling in this basic setup—it simply sends a fixed response for every request. This is how many developers start learning server fundamentals.

Handling GET and POST Requests
Once you're familiar with the basics, the next step is to handle different HTTP methods, like GET and POST.

GET is used to retrieve data. For example, when a user opens a page or fetches information from your server, a GET request is made.

POST is used to send data to the server. This is typically used in forms, API calls, or when submitting any kind of user input.
_

Node.js Server for Postman Testing (GET & POST)

const http = require('http');

const PORT = 3000;

const server = http.createServer((req, res) => {
  // Handle GET request to /api/hello
  if (req.method === 'GET' && req.url === '/api/hello') {
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ message: 'Hello from Node.js server!' }));

  // Handle POST request to /api/data
  } else if (req.method === 'POST' && req.url === '/api/data') {
    let body = '';

    // Listen for data chunks
    req.on('data', chunk => {
      body += chunk.toString();
    });

    // Finished receiving data
    req.on('end', () => {
      try {
        const parsedData = JSON.parse(body); // Convert JSON string to object
        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({
          message: 'Data received successfully!',
          yourData: parsedData
        }));
      } catch (error) {
        res.writeHead(400, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ error: 'Invalid JSON data' }));
      }
    });

  // Handle unknown routes
  } else {
    res.writeHead(404, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ error: 'Route not found' }));
  }
});

server.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Enter fullscreen mode Exit fullscreen mode

How to Test in Postman

  1. Test the GET route Method: GET

URL: http://localhost:3000/api/hello
Expected Response:

{
  "message": "Hello from Node.js server!"
}

Enter fullscreen mode Exit fullscreen mode

"For a more detailed explanation, refer to the official documentation."
Reference : https://www.postman.com/api-platform/api-testing/
https://share.google/images/v5EoLNZMRBFBQX6pj
https://share.google/images/QByzuPAbnnMdlYLxA

Top comments (0)