Basics about Backend

Geetika Bajpai - Aug 1 - - Dev Community

Backend has 2 major components:-
1. A Programming Language:- Java, javascript, php, golang, c++ etc. ( Almost all these languages use some framework or library).
2. A Database:- Mongo, MySQL, Postgres, sqlite etc. (Similarly, you will use an ORM (Object-Relational Mapping) or ODM (Object Document Mapping) when interacting with databases. Names like Prisma and Mongoose will come up).

Image description

Frontend (Browser and Mobile):Users interact with the application through the browser or mobile app.

API (Application Programming Interface):Acts as an intermediary, allowing the frontend to communicate with the backend server.

Backend:Processes requests from the frontend, interacts with the database, and sends responses back.
Contains the logic and functions needed to handle specific tasks like user authentication.

Database:Stores application data and is located in another continent for better performance and redundancy.

When dealing with backend development, you primarily handle three types of tasks:

1.Data Handling:

  • The most common task in backend development is processing data received from the frontend.
  • Example: When a user tries to log in, the frontend sends the username and password to the backend. The backend verifies these credentials against the database and sends a response back to the frontend.

2.File Handling:

  • Backends often need to handle file uploads and downloads. This can include storing files on the server or retrieving them for user access.
  • Example: A user uploads a profile picture. The backend saves this image to the server and updates the user's profile to reference the uploaded file.

3.Third-Party API Integration:

  • Backends frequently interact with third-party services to extend functionality.
  • Example: To send an email confirmation, the backend may use an email service provider's API. The backend sends the necessary data (recipient email, subject, message body) to the third-party API, which then handles the email sending.

In modern JavaScript environments, there are several options for backend development beyond the traditional Node.js. Here are the key points:

Multiple Runtime Environments:

  • Historically, learning backend development in JavaScript often started with Node.js.
  • Nowadays, there are alternative runtime environments like Deno and Bun.
  • You can even mix environments, using part of your backend on Node.js and part on Deno or Bun.

The file structure I am referring to is a common organization pattern used in backend development, regardless of the specific programming language or framework. Here's a breakdown of the typical components based on the content you provided:

1. Root Directory

index.js or main.js: This is the entry point of your application. It’s where you initialize and start your application, connect to the database, and set up basic configurations.

2. Folders and Files

app.js:This file typically contains the main application logic and configuration settings, such as middleware setup, routes configuration, and application-level settings.

config/:

Configuration Files:These files include environment-specific settings such as database connections, API keys, and other constants.
Example: config.js, databaseConfig.js.

constants/:

constants.js:Contains immutable values or configurations used throughout the application. This can include things like status codes, roles, or types. For instance, if you're building a booking application, constants might include seat types (e.g., window, middle, aisle).

models/:

Model Definitions:Defines the schema for the data stored in your database. This is where you set up how your data is structured and validated.
Example:userModel.js, productModel.js.

controllers/:

Controller Logic: Contains functions or methods that handle business logic. Controllers manage the requests and responses, processing data and interacting with models.
Example: userController.js, productController.js.

routes/:

Routing Definitions:Maps URLs to specific controller actions. This determines how different HTTP requests (GET, POST, etc.) are routed to the appropriate controllers.
Example:userRoutes.js, productRoutes.js.

middlewares/:

Middleware Functions:Contains functions that execute during the request-response cycle, such as authentication checks, logging, or data validation.
Example: authMiddleware.js, errorHandlingMiddleware.js.

utils/:

Utility Functions: Houses reusable functions or helpers used across the application, such as email sending functions or file upload handlers.
ExampleemailHelper.js, fileUploadHelper.js.

database/:

Database Initialization:Contains scripts or configurations related to database setup and connections.
Example:dbConnection.js, dbSetup.js.

`views`/ (if applicable):

Template Files:Used if the application includes server-side rendering. This directory contains the view templates or files used to generate HTML content.
Example: index.ejs, userProfile.ejs.

3. Other Files

  1. package.json: Manages dependencies and scripts for the project.
  2. .env: Stores environment variables like database credentials or API keys.
  3. .gitignore: Specifies which files and directories should be ignored by version control
. . . . . . . . . . . . . .
Terabox Video Player