Node.js, Express.js & MongoDB Interview Cheat Sheet
Node.js, Express.js & MongoDB Interview Cheat Sheet
Node.js (Basic)
1. What is Node.js?
- Node.js is a runtime to run JavaScript on the server side.
2. What is the event loop?
- A mechanism that handles asynchronous operations in Node.js.
3. What are callbacks?
- Functions passed to other functions for asynchronous handling.
Example:
fs.readFile('file.txt', (err, data) => { if (err) return; console.log(data.toString()); });
4. Difference between synchronous and asynchronous:
- Sync blocks execution. Async uses callbacks/promises to avoid blocking.
Express.js (Basic)
1. What is Express.js?
- A lightweight web framework for Node.js.
2. Creating a basic server:
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);
3. What is middleware?
- Functions executed during request processing.
MongoDB (Basic)
1. What is MongoDB?
- A NoSQL database using JSON-like documents.
2. Connecting MongoDB:
mongoose.connect('mongodb://localhost:27017/dbname');
3. CRUD with Mongoose:
const User = mongoose.model('User', new mongoose.Schema({ name: String }));
User.find(); User.findOne(); User.findById();
Intermediate Questions
1. Error handling in Express:
app.use((err, req, res, next) => { res.status(500).send('Error') });
2. Route parameters:
app.get('/user/:id', (req, res) => res.send(req.params.id));
3. Embedded vs Reference documents:
- Embedded: nested in document.
- Reference: use ObjectId to reference another collection.
Scenario-based Q&A
1. REST API Example:
- POST /users to create, GET /users to read data.
2. CORS in Express:
const cors = require('cors'); app.use(cors());
3. Helmet + Rate Limiting:
app.use(helmet()); app.use(rateLimit({ windowMs: 15*60*1000, max: 100 }));
4. JWT Authentication:
jwt.sign(payload, secret); jwt.verify(token, secret, callback);
Comments
Post a Comment