Node.js Fundamentals

1. Asynchronous Behavior of Node, Callback Functions

Node.js uses an event-driven, non-blocking I/O model for asynchronous operations. Callbacks are functions passed as arguments to handle results after an async operation completes.

fs.readFile('file.txt', (err, data) => {
    if (err) throw err;
    console.log(data);
});

2. Create HTTP Server in Node.js, Handle HTTP Methods

Use the http module:

const http = require('http');
const server = http.createServer((req, res) => {
    if (req.method === 'GET') {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('GET request');
    } else if (req.method === 'POST') {
        res.end('POST request');
    }
});
server.listen(3000);

3. Read Files Using File System Module, Blocking & Non-Blocking

Use fs module:

4. What is npm, Node Modules, Why Important?

5. Why Node is Lightweight?

Node is lightweight due to its non-blocking, event-driven architecture and V8 engine, which minimizes overhead and handles many concurrent connections efficiently.

Express Framework

6. Core Features of Express Framework

Express features include middleware support, routing, HTTP utilities, and template engines for building RESTful APIs and web apps.