Published on

Understanding server applications and building with Express

Authors
  • avatar
    Name
    hwahyeon
    Twitter

A server can generally be thought of as a computer that is always running to process requests. (However, not all servers are always active—some are only activated when needed.) While it lacks peripherals like a keyboard or display, it is a computer that is constantly connected to the internet.

A server is in a "listening" state, meaning it waits for client requests and prepares to respond. For a server to fulfill this role, it must be set to listen for requests on a specific port. Before diving into how this works, let's first understand what a server application is.

What is a Server Application?

A server application is software that runs on a server, processing client requests and returning responses. While the term "server" often refers to the physical hardware or network infrastructure, a server application refers to the program or service operating on the server.

The Role of a Server Application:

1. Receiving Requests:

The server receives HTTP requests (e.g., GET, POST) from clients such as web browsers or mobile apps. These requests may include information like a URL and headers, and in some cases, a body (e.g., for POST or PUT requests).

2. Processing Requests:

The server performs operations based on the request, such as:

  • Fetching data from a database.
  • Handling file uploads.
  • Calling another API.

This step involves executing business logic or transforming data as needed.

3. Returning Responses:

The server sends the results of the processing back to the client, which may include:

  • Status codes (e.g., 200 OK, 404 Not Found, 500 Internal Server Error).
  • Body data (HTML, JSON, files, etc.).
  • Headers (e.g., Content-Type, Cache-Control).

4. Managing State:

In stateful systems, the server maintains user state via sessions, authentication tokens, or other mechanisms during interactions with the client — however, in RESTful architectures, the server is typically stateless and relies on the client to provide necessary context with each request.

Express and Server Applications

Express is a framework built on Node.js that simplifies the creation of server applications.

Example Code:

import express from 'express'
const PORT = 4000
const app = express()
const handleListening = () => console.log(`Server listening on port http://localhost:${PORT}`)
app.listen(PORT, handleListening)

This code uses Express to create an application (app) and configures it to listen for requests on the specified port. When the server starts, the handleListening function is called to indicate that the server is ready. The server is now listening for client requests. However, since no routes have been defined yet, it does not perform any actions when a request is received. If a client sends a request, the server will respond with a 404 status code and the message 'Cannot GET /route'.

Anyway, the server is running.