What is [Link]?
Content:
•[Link] is an open-source, cross-platform JavaScript runtime environment.
•It allows running JavaScript on the server side.
•Built on the Google Chrome V8 JavaScript engine.
V8 Engine:
Content:
•V8 is Google’s open-source JavaScript engine used in Chrome.
•Compiles JavaScript directly to native machine code.
•[Link] uses V8 to execute JavaScript outside the browser.
npm – Node Package Manager:
Content:
•Default package manager for [Link].
•Over 2 million packages available.
•Use it to install libraries and tools:
[Link] Modules:
Content:
•Modular architecture: code is organized into reusable components.
•Types of modules:
•Core modules (e.g., http, fs)
•Local modules (your own code)
•Third-party modules (installed via npm)
Code Example:
const fs = require('fs');
[Link]('[Link]', (err, data) => {
if (err) throw err;
[Link]([Link]());
});
Creating a Simple HTTP Server:
Content:
•Use the built-in http module.
•Minimal setup — no external dependencies.
•Listens to client requests and sends responses.
Basic HTTP Server Example:
const http = require('http');
const server = [Link]((req, res) => {
[Link](200, {'Content-Type': 'text/plain'});
[Link]('Hello, World!\n');
});
[Link](3000, () => {
[Link]('Server running at [Link]
});
Key Components Explained:
•[Link]() – Creates the server.
•req – The request object (incoming data from the client).
•res – The response object (used to send data back).
•[Link](statusCode, headers) – Sets status and headers.
•[Link]() – Ends the response and sends data.
What is ExpressJS?:
•Minimal and flexible [Link] web application framework.
•Makes building APIs and web servers easier and cleaner.
•Built on top of the http module.
Installing Express:
Content:
Basic Server Example:
const express = require('express');
const app = express();
[Link]('/', (req, res) => {
[Link]('Hello from Express!');
});
[Link](3000, () => {
[Link]('Server is running on port 3000');
});
Routing in Express:
Content:
•Routes define how an app responds to HTTP requests.
•Based on path and HTTP method (GET, POST, etc.).
Example:
[Link]('/about', (req, res) => {
[Link]('About Page');
});
[Link]('/submit', (req, res) => {
[Link]('Form Submitted');
});
What is Middleware?
Content:
•Middleware functions have access to:
req, res, and next()
•Used for:
Logging, parsing JSON, authentication, error handling
Basic Example:
[Link]((req, res, next) => {
[Link](`${[Link]} ${[Link]}`);
next();
});
Built-in & Third-Party Middleware:
Examples
•Built-in:[Link]() – Parses incoming JSON.
•Third-party:
•morgan – Logging
•cors – Enable cross-origin resource sharing
Example:
const cors = require('cors');
[Link](cors());
File Handling in [Link]:
Content:
•[Link] provides the fs module to interact with the file system.
•Supports both synchronous and asynchronous operations.
•Common operations:
•Reading files
•Writing files
•Updating and deleting files
Importing the fs Module:
Code:
const fs = require('fs');
Note:
•No installation needed — fs is a built-in core module in [Link].
Reading a File:
Code Example (Asynchronous):
[Link]('[Link]', 'utf8', (err, data) => {
if (err) throw err;
[Link](data);
});
Code Example (Synchronous):
const data = [Link]('[Link]', 'utf8');
[Link](data);
Append to File:
Asynchronous Write:
[Link]('[Link]', 'Hello Node!', (err) => {
if (err) throw err;
[Link]('File written!');
});
Writing to a File:
[Link]('[Link]', '\nNew line added.', (err)
=> {
if (err) throw err;
[Link]('Content appended!');
});
Deleting and Renaming Files
Delete File:
[Link]('[Link]', (err) => {
if (err) throw err;
[Link]('File deleted!');
});
Rename File:
[Link]('[Link]', '[Link]', (err) => {
if (err) throw err;
[Link]('File renamed!');
});
Introduction to Databases in [Link] Content:
● Databases are used to store, manage, and retrieve data.
● Two common types:
○ MongoDB (NoSQL, document-based)
○ SQLite (SQL-based, file-based relational database)
● [Link] supports both via libraries.
What is MongoDB?
Content:
● NoSQL database (stores data in JSON-like documents).
● Schema-less and flexible.
● Uses collections and documents instead of tables and rows.
Connecting to MongoDB with Mongoose:
Install Mongoose:
npm install mongoose
Basic Connection:
const mongoose = require('mongoose');
[Link]('mongodb://localhost:27017/mydb')
.then(() => [Link]('MongoDB Connected'))
.catch(err => [Link](err));
Defining a Mongoose Schema & Model:
What is a Schema?
A Schema defines the structure of documents within a MongoDB collection.
What is a Model?
A Model is a constructor compiled from a Schema.
Example
const userSchema = new [Link]({
name: String,
age: Number
});
const User = [Link]('User', userSchema);
What is SQLite?
Content:
● Lightweight, file-based relational database.
● Stores all data in a single .sqlite or .db file.
● No separate server needed.
Install SQLite3:
npm install sqlite3
const sqlite3 = require('sqlite3').verbose();
const db = new [Link]('[Link]');
[Link](() => {
[Link]("CREATE TABLE users (name TEXT, age INT)");
[Link]("INSERT INTO users (name, age) VALUES (?, ?)", ['Bob', 30]);
});
MongoDB vs SQLite – Quick Comparison
Feature MongoDB SQLite
Type NoSQL SQL
Data Format Documents (JSON) Tables (rows/columns)
Schema Flexible Fixed schema
Setup Needs server File-based
Best For APIs, scalable apps Desktop, small apps
Building a Simple RESTful API with [Link] and Express
What is a RESTful API?
Content:
● REST = Representational State Transfer
● A design pattern for building APIs using HTTP methods:
○ GET – Read data
○ POST – Create data
○ PUT/PATCH – Update data
○ DELETE – Remove data
Setting Up Express for an API
Code Example:
npm install express
const express = require('express');
const app = express();
[Link]([Link]()); // Middleware to parse JSON
[Link](3000, () => {
[Link]('API server running on port 3000');
});
Testing the API
Use tools like:
● Postman
● cURL
● Browser (for GET requests)
Example POST request in Postman:
● URL: [Link]
● Method: POST
● Body (JSON):
RESTful API – Summary
HTTP Method Endpoint Description
GET /users Get all users
POST /users Create new user
PUT /users/:id Update a user
DELETE /users/:id Delete a userz