Node.js & Express.
js Beginner to Advanced Guide
Lesson 1: Introduction to Node.js
What is Node.js?
- Node.js is a JavaScript runtime built on Chrome's V8 engine.
- It allows you to run JavaScript on the server side.
Benefits:
- Fast and lightweight
- Single language (JavaScript) for both client and server
- Asynchronous and non-blocking
Example: Basic Node Server
----------------------------
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("Hello from Node.js server!");
});
server.listen(3000, () => console.log("Server running at http://localhost:3000"));
Lesson 2: Node.js Modules & File System
Modules in Node.js:
- Built-in modules (e.g., fs, http, path)
- Custom/user-defined modules
- Third-party modules (via npm)
Creating a module:
----------------------------
math.js:
function add(a, b) { return a + b; }
module.exports = { add };
app.js:
const math = require('./math');
console.log(math.add(2, 3));
FS Module (File System):
----------------------------
Read File:
fs.readFile("file.txt", "utf-8", (err, data) => { ... });
Write File:
fs.writeFile("file.txt", "Hello", (err) => { ... });
Append File:
Node.js & Express.js Beginner to Advanced Guide
fs.appendFile("file.txt", "More text", (err) => { ... });
Delete File:
fs.unlink("file.txt", (err) => { ... });