Q1. What are the most commonly used built-in modules in Node.js?
Common core modules include: fs (file system), http (HTTP server/client), path (path manipulation), os (operating system info), events (event emitter), stream (streaming data), crypto (cryptographic functions), util (utility functions), and url (URL parsing). They're available without installation.
Q2. How do you use the http module?
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200);
res.end('Hello');
});
server.listen(3000);
The http module provides low-level HTTP functionality for creating servers and making client requests.Q3. What is the util module used for?
The util module provides utility functions: util.promisify() converts callback-based functions to promise-based, util.inspect() for debugging (like console.dir with more options), util.format() for string formatting, and util.types for type checking. It's helpful for development and debugging.
Q4. How does the url module work?
The url module parses and formats URLs.
const { URL } = require('url');
const myUrl = new URL('https://example.com/path?query=1');
console.log(myUrl.pathname);
It provides both legacy url.parse() and modern WHATWG URL API. Useful for handling request URLs in servers.Q5. What is the crypto module used for?
crypto provides cryptographic functionality: creating hashes (
crypto.createHash('sha256').update(data).digest('hex')
), generating random bytes, encryption/decryption, and creating HMACs. It's essential for security features like password hashing, token generation, and data encryption.