Node.js Flashcards

(30 cards)

1
Q

What is Node.js and how does it work?

A

Node.js is a runtime that lets you run JavaScript on the server. It works using an event loop to handle tasks asynchronously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between CommonJS and ES Modules?

A

CommonJS uses require() and module.exports, while ES Modules use import and export, following modern JavaScript standards.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you initialize a Node.js project?

A

Run ‘npm init’ in your terminal to create a package.json file for your project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is npm and how is it used?

A

npm is a package manager for Node.js. Use it to install packages with ‘npm install package-name’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Express.js and why is it commonly used with Node.js?

A

Express.js is a framework that makes it easy to build web apps and APIs with Node.js due to its simplicity and flexibility.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is middleware in Express?

A

Middleware are functions that run during the request-response cycle, like logging or authentication.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you handle routing in Express?

A

Use app.get(), app.post(), etc., with a path and callback, e.g., app.get(‘/home’, (req, res) => res.send(‘Home’)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between req.params

A

req.query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you send a JSON response from an Express route?

A

Use res.json({ key: value }), e.g., res.json({ message: ‘Success’ }).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you handle errors in Express?

A

Use a middleware with (err, req, res, next) => res.status(500).send(err) to catch and respond to errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Event Loop in Node.js?

A

The event loop manages asynchronous tasks, allowing Node.js to handle many operations without blocking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are Streams in Node.js and when should you use them?

A

Streams handle data in chunks, useful for large files or real-time data like video streaming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between synchronous and asynchronous code in Node.js?

A

Synchronous code runs one at a time, blocking others, while asynchronous code runs in the background, like file reads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do Promises and async/await work in Node.js?

A

Promises handle async tasks with .then/.catch, and async/await makes it look like sync code with try/catch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you connect a Node.js app to a MongoDB database using Mongoose?

A

Use mongoose.connect(‘mongodb://localhost/dbname’) and define schemas to interact with the database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are environment variables and how are they managed in a Node.js app?

A

Environment variables store config data (e.g., API keys). Manage them with a .env file and the dotenv package.

17
Q

How do you implement user authentication in Express?

A

Use middleware like passport.js with strategies (e.g., local or JWT) to verify user logins.

18
Q

What is CORS and how do you handle it in Express?

A

CORS is a security feature blocking cross-origin requests. Handle it with the cors middleware.

19
Q

How do you structure a scalable Node.js project?

A

Organize into folders like routes, controllers, and models, keeping code modular and reusable.

20
Q

How do you handle file uploads in Node.js?

A

Use the multer middleware to process and save uploaded files from forms.

21
Q

How does Node.js handle concurrency with a single-threaded architecture?

A

It uses the event loop and libuv to handle many connections concurrently without multiple threads.

22
Q

What is clustering in Node.js and when should you use it?

A

Clustering uses multiple processes to use all CPU cores, useful for high-traffic apps.

23
Q

How do you handle rate limiting and brute-force protection in Express apps?

A

Use packages like express-rate-limit to limit requests and prevent brute-force attacks.

24
Q

What are WebSockets and how are they implemented in Node.js?

A

WebSockets provide real-time two-way communication. Implement with the ws library or Socket.io.

25
How do you create RESTful APIs using Express?
Design APIs with standard HTTP methods (GET, POST, etc.) and clear endpoints, e.g., /api/users.
26
How do you create and verify JWT tokens for authentication?
Create with jsonwebtoken.sign(), verify with jsonwebtoken.verify() using a secret key.
27
What is the difference between MVC and layered architecture in backend apps?
MVC separates Model, View, Controller; layered architecture splits into data, business, and presentation layers.
28
How do you implement input validation and sanitization in Express?
Use express-validator to check and clean input data before processing.
29
How do you handle logging and monitoring in a Node.js production environment?
Use Winston or Bunyan for logging, and tools like New Relic for monitoring performance.
30
What are some best practices for securing a Node.js application?
Use HTTPS, sanitize inputs, limit headers with Helmet, and keep dependencies updated.