"Node.js introduced me to the world of backend development and server-side scripting."
Node.js is a runtime environment that allows developers to write server-side applications using JavaScript. With Node.js, I explored building RESTful APIs, handling server requests, and managing databases.
Introduction to Node.js:
Setting up a Node.js environment.
Understanding modules (require and exports) and npm.
// Setting up Node.js Environment
console.log("Node.js is running!");
// Modules
const fs = require("fs"); // Built-in module
fs.writeFileSync("example.txt", "Hello, Node.js!");
// Exporting and Importing
// file1.js
module.exports.greet = () => console.log("Hello from module!");
// file2.js
const { greet } = require("./file1");
greet();
HTTP and Express.js:
Setting up servers using the built-in http module.
Using Express.js to create routes and handle requests.
// HTTP Module Server
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello, World!");
});
server.listen(3000, () => console.log("Server running on port 3000"));
// Express.js Server
const express = require("express");
const app = express();
app.get("/", (req, res) => res.send("Hello, Express!"));
app.listen(3000, () => console.log("Express server running on port 3000"));
Middleware:
Using middleware functions in Express for logging, authentication, and data validation.
// Middleware Example
const app = express();
// Logger Middleware
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
// Authentication Middleware
const authenticate = (req, res, next) => {
const token = req.headers.authorization;
if (token === "12345") {
next();
} else {
res.status(401).send("Unauthorized");
}
};
app.use(authenticate);
Working with APIs:
Creating RESTful APIs for CRUD operations.
Handling JSON data and requests using body-parser or Express’s built-in middleware.
// RESTful API with CRUD Operations
const app = express();
app.use(express.json()); // Built-in middleware for JSON parsing
let data = [];
// Create
app.post("/data", (req, res) => {
data.push(req.body);
res.status(201).send("Data added!");
});
// Read
app.get("/data", (req, res) => res.json(data));
// Update
app.put("/data/:id", (req, res) => {
const id = req.params.id;
data[id] = req.body;
res.send("Data updated!");
});
// Delete
app.delete("/data/:id", (req, res) => {
const id = req.params.id;
data.splice(id, 1);
res.send("Data deleted!");
});
app.listen(3000, () => console.log("API running on port 3000"));
Database Integration:
Connecting to databases like SQL.
// Connecting to SQL Database
const mysql = require("mysql");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "exampleDB",
});
connection.connect((err) => {
if (err) throw err;
console.log("Connected to SQL database!");
// Sample Query
connection.query("SELECT * FROM users", (err, results) => {
if (err) throw err;
console.log(results);
});
});