MongoDB
MongoDB
Express.js
1.What is React and why would you use it over other frameworks?
React is a JavaScript library for building UI components. It offers fast
rendering through the Virtual DOM, component-based architecture, and
unidirectional data flow, making it more efficient than traditional frameworks.
Node.js Questions
- *Event Loop:* The core mechanism in Node.js that listens for events (e.g.,
HTTP requests, file system operations) and executes callback functions when an
event occurs.
- *Event Emitter:* Node.js has an EventEmitter module that allows applications
to define and listen for events.
*Example:*
javascript
const EventEmitter = require('events');
const event = new EventEmitter();
event.on('greet', () => {
console.log('Hello, Welcome to Node.js!');
});
Example of *require*:
javascript
const fs = require('fs');
Example of *import*:
javascript
import fs from 'fs';
---
4. How do you handle asynchronous operations in Node.js?
Node.js supports several ways to handle *asynchronous operations*:
2. *Promises*
javascript
const fs = require('fs').promises;
fs.readFile('file.txt', 'utf8')
.then(data => console.log(data))
.catch(err => console.error(err));
---
Example:
json
{
"name": "my-node-app",
"version": "1.0.0",
"description": "A sample Node.js application",
"dependencies": {
"express": "^4.18.2"
},
"scripts": {
"start": "node index.js"
}
}
To install dependencies:
npm install
---
1. Explain how data flows from the front-end (React) to the back-end
(Express/Node) and to the database (MongoDB).
In a *MERN stack* application, data flows in the following way:
1. Front-end (React)
- React makes an API request (e.g., fetch or Axios) to the backend.
- Example:
javascript
fetch('http://localhost:5000/api/users')
.then(res => res.json())
.then(data => console.log(data));
2. *Backend (Express/Node)*
- Express receives the request and processes it.
- Example:
javascript
app.get('/api/users', async (req, res) => {
const users = await User.find(); // Fetching from MongoDB
res.json(users);
});
3. *Database (MongoDB)*
- The backend queries MongoDB using *Mongoose* and sends the result
back to the frontend.
---
1. *User Registration*
javascript
const bcrypt = require('bcrypt');
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const newUser = new User({ username: req.body.username, password:
hashedPassword });
await newUser.save();
3. *Protecting Routes*
javascript
const authMiddleware = (req, res, next) => {
const token = req.header('Authorization');
if (!token) return res.status(401).send('Access Denied');
try {
const verified = jwt.verify(token, 'secret_key');
req.user = verified;
next();
} catch (err) {
res.status(400).send('Invalid Token');
}
};
---
### *3. What are some common performance optimization techniques for a
MERN stack application?*
- *Optimize MongoDB Queries:* Use indexes (.createIndex()).
- *Use Caching:* Redis or in-memory caching.
- *Pagination & Lazy Loading:* Avoid fetching large datasets.
- *Optimize React Rendering:* Use React.memo, PureComponent, and avoid
unnecessary re-renders.
- *Use Load Balancers & Clustering:* Handle multiple requests efficiently.
---
### *4. Can you describe a challenging problem you encountered in a MERN
stack project?*
One common challenge in MERN applications is *handling large data sets
efficiently*.
*Problem:*
A dashboard with *thousands of records* was taking too long to load.
*Solution:*
- Implemented *pagination* with MongoDB’s .skip() and .limit().
- Used *server-side filtering* instead of fetching all data at once.
- Applied *Redis caching* to reduce database queries.
Example:
javascript
const users = await User.find().skip((page - 1) * limit).limit(limit);
---