What is Node.js and how does it work?
Node.js is a runtime that lets you run JavaScript on the server. It works using an event loop to handle tasks asynchronously.
What is the difference between CommonJS and ES Modules?
CommonJS uses require() and module.exports, while ES Modules use import and export, following modern JavaScript standards.
How do you initialize a Node.js project?
Run ‘npm init’ in your terminal to create a package.json file for your project.
What is npm and how is it used?
npm is a package manager for Node.js. Use it to install packages with ‘npm install package-name’.
What is Express.js and why is it commonly used with Node.js?
Express.js is a framework that makes it easy to build web apps and APIs with Node.js due to its simplicity and flexibility.
What is middleware in Express?
Middleware are functions that run during the request-response cycle, like logging or authentication.
How do you handle routing in Express?
Use app.get(), app.post(), etc., with a path and callback, e.g., app.get(‘/home’, (req, res) => res.send(‘Home’)).
What is the difference between req.params
req.query
How do you send a JSON response from an Express route?
Use res.json({ key: value }), e.g., res.json({ message: ‘Success’ }).
How do you handle errors in Express?
Use a middleware with (err, req, res, next) => res.status(500).send(err) to catch and respond to errors.
What is the Event Loop in Node.js?
The event loop manages asynchronous tasks, allowing Node.js to handle many operations without blocking.
What are Streams in Node.js and when should you use them?
Streams handle data in chunks, useful for large files or real-time data like video streaming.
What is the difference between synchronous and asynchronous code in Node.js?
Synchronous code runs one at a time, blocking others, while asynchronous code runs in the background, like file reads.
How do Promises and async/await work in Node.js?
Promises handle async tasks with .then/.catch, and async/await makes it look like sync code with try/catch.
How do you connect a Node.js app to a MongoDB database using Mongoose?
Use mongoose.connect(‘mongodb://localhost/dbname’) and define schemas to interact with the database.
What are environment variables and how are they managed in a Node.js app?
Environment variables store config data (e.g., API keys). Manage them with a .env file and the dotenv package.
How do you implement user authentication in Express?
Use middleware like passport.js with strategies (e.g., local or JWT) to verify user logins.
What is CORS and how do you handle it in Express?
CORS is a security feature blocking cross-origin requests. Handle it with the cors middleware.
How do you structure a scalable Node.js project?
Organize into folders like routes, controllers, and models, keeping code modular and reusable.
How do you handle file uploads in Node.js?
Use the multer middleware to process and save uploaded files from forms.
How does Node.js handle concurrency with a single-threaded architecture?
It uses the event loop and libuv to handle many connections concurrently without multiple threads.
What is clustering in Node.js and when should you use it?
Clustering uses multiple processes to use all CPU cores, useful for high-traffic apps.
How do you handle rate limiting and brute-force protection in Express apps?
Use packages like express-rate-limit to limit requests and prevent brute-force attacks.
What are WebSockets and how are they implemented in Node.js?
WebSockets provide real-time two-way communication. Implement with the ws library or Socket.io.