Backend-Notes
Backend-Notes
js
In web development, creating functional applications requires three core components: Frontend, Backend, and
Database. Think of it as a restaurant where the frontend is the menu and waiter, the backend is the kitchen, and the
database is the storage of ingredients.
Frontend
The Frontend is what users interact with directly on a website. It includes all the visuals, layouts, and interfaces that
users can see and click on. This part of the application runs in the browser and is built using HTML, CSS, and
JavaScript.
Backend
The Backend is the “kitchen” of the application. It handles all the behind-the-scenes tasks, like processing user
requests, running the core logic, and communicating with the database. For instance, when you log into a website,
the backend checks your credentials, accesses the database, and sends the appropriate response.
Database
The Database is where data is stored and managed, like storing user profiles, messages, and other necessary
information. In our restaurant analogy, it’s the storage room where all ingredients are kept for quick access.
2. Client-Server Model
The Client-Server Model is a foundational concept in web development. Here’s how it works:
1. Client (Frontend): The client, typically a browser, sends a request to the server, like asking for data to display
on a page.
2. Server (Backend): The server processes this request, runs any required code, retrieves data from the
database, and returns a response.
For example, if you search for a product, the client sends a request to the backend. The backend retrieves relevant
data from the database, processes it, and responds to the client.
Request: The action the client asks the server to perform, often done through an API.
Response: The server's answer to the client's request, including the requested data or an error message.
API Endpoint: A specific address on the backend server where the client sends requests,
like api.website.com/products.
Business Logic: Handling the main functionality, such as calculating prices or processing orders.
While many languages like Python, Ruby, and Java are popular for backend development, JavaScript has also become
a major player in backend development through Node.js. Unlike the frontend, the backend doesn't have a browser to
execute JavaScript. Node.js acts as a JavaScript runtime, allowing developers to run JavaScript directly on the server.
Why Node.js?
JavaScript Everywhere: Frontend and backend can both be written in JavaScript, simplifying development.
Non-Blocking I/O: Node.js handles requests efficiently, making it excellent for scalable, fast applications.
Node.js was created by Ryan Dahl in 2009 to make server-side JavaScript a reality. It uses Google’s V8 engine, which
is also used in Chrome, to convert JavaScript code into fast, machine-readable code.
The core code of Node.js is open-source and has grown to be a popular choice for backend developers due to its
speed, efficiency, and a vast ecosystem of modules.
// greet.js
3. node greet.js
Modules in Node.js help us organize code by splitting it into reusable parts. Node uses CommonJS, which defines
how modules work.
Example:
// square.js
function square(num) {
return num * num;
}
module.exports = square;
// app.js
const square = require('./square');
console.log(square(5)); // Output: 25
8. Node.js Modules Superpowers: npm and Built-in Modules
npm (Node Package Manager): A vast repository of external modules you can install and use in your
projects.
The fs module allows for interaction with the file system, such as reading, writing, or deleting files.
const fs = require('fs');
fs.writeFileSync('message.txt', 'Hello, Node.js!');
console.log('File created successfully');
Let’s build a simple Command Line Interface (CLI) tool using Node’s fs module to count words in a file.
Step-by-Step Guide
Use process.argv to get file name input from the command line.
// wordCounter.js
const fs = require('fs');
try {
const data = fs.readFileSync(fileName, 'utf8');
const wordCount = data.split(/\s+/).length;
console.log(`Word count: ${wordCount}`);
} catch (error) {
console.error('Error reading file:', error.message);
}
The Frontend is the user interface, the Backend processes requests, and the Database stores data.
The Client-Server Model defines how requests and responses are handled between frontend and backend.
Node’s CommonJS module system and npm provide robust module management.
Backend - Handles business logic, storing and retreiving data, auth etc;
Ryan Dhal - why not make JS run outside the browser in a computer;
^ Advantages - JS can be used for full stack - same developer who knows only html, css and js can also
do the backend;
Chrome - v8 engine;
Firefox - spidermonkey;
He took out the (v8 engine + added a few more things as required) to run JS outside the browser;
Node - Runtime environment which helps run Javascript outside the browser;
//Modules
^ A part of code which is reusable, we can have it in separate file;
Modules :
1. Custom modules - where we wrote our own code - hello.js, calc.js etc
2. Inbuilt node modules as well
// console.log(welcome_message)
// const fs = require("fs")
// fs.appendFileSync("./output.txt", output)