Introduction to Backend and Web servers:

·

3 min read

Role of backend

Backend plays a very important role in applications whether they are for web or mobile. The backend is responsible for handling server-side operations, processing data, and communicating with databases. It ensures that the users get all the functionalities that they expect from the app fulfilled. Thus, while the frontend is an essential part of an application we cannot ignore the backend which can be understood as the brains of the application.

Client vs Server Architecture

I am learning the backend for building web apps, so in the context of the web, every app has two major sides of architecture: client and server architecture.

The above diagram breaks down both architectures simply and easily. Let's deep dive into them,

Client Architecture:

The client is the front-end part of the application that interacts directly with the user. It runs on the user's device (e.g., computer, smartphone) and is responsible for:

  1. Rendering the visual elements that users interact with, such as buttons and text.

  2. Capturing user input

  3. Sending requests to the server for data or actions

Server Architecture:

The server is the back-end part of the application that handles the business logic, data storage, and processing. It runs on a remote machine or in the cloud and is responsible for:

  1. Handling incoming requests from clients, performing necessary computations, and preparing responses.

  2. Storing, retrieving, and managing data in databases.

  3. Authenticating users, authorizing access, and ensuring data security.

Node.js and servers

Node.js is a javascript runtime built on Chrome's V8 engine which enables to use of javascript to build apps beyond the browser. Server as stated above is a program that enables the frontend to serve the requests made by the users. It also helps in communicating with the database. There are many uses of node other than building servers, we can actually use node to interact with the system filesystem and much more which is not what we would be focusing on now. With that said, Let's now build a server using node.js

Building a server in node.js

Setting up a Node.js development environment is relatively straightforward. Once you have Node.js installed, you can create a basic server using the built-in http module. This server can handle incoming HTTP requests and send responses back to the client.

Here's an example of a simple Node.js server:

const http = require("http");

const hostname = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {
  if (req.url === "/") {
    res.statusCode = 200;
    res.setHeader("Content-Type", "text/plain");
    res.end("Hello Backend");
  } else if (req.url === "/about") {
    res.statusCode = 200;
    res.setHeader("Content-Type", "text/plain");
    res.end("Starting backend with building a server");
  } else {
    res.statusCode = 404;
    res.setHeader("Content-Type", "text/plain");
    res.end("404 Not Found");
  }
});

server.listen(port, hostname, () => {
  console.log(`Server is listening at http:// ${hostname}:${port}`);
});

When you run this program in your terminal, you get the message that the server is listening at the hostname and port which are hard-coded for now. This is the process of building a server in node.js

Conclusion

While we learned the server architecture and what it accomplishes, we also got to learn about servers, node and got to build a server ourselves in node. An important thing to note here is that this process gets frustrating and troublesome when there are a lot of routes in the app which is generally the case. Thus, we need some help which can help to solve this problem and thankfully, we have it in the form of a framework called Express which simplifies the process of building servers. We will study Express in detail in the next article. Till then,

Keep Coding!