🎯 Master Your Career

Interview-Bytes

Expertly curated technical interview questions,
simplified into manageable lessons.

Showing 11–20 of 22 questions

11

💡 What are Callbacks in Node.js?

Callbacks are functions passed as argument to another function and gets executed after the main func has completed its task. In node.js they are crucial for non-blocking behaviour, callbacks are passed to handle async tasks.

How it works ? The async task starts → Node.js keeps working → When done, the event loop triggers the callback to handle the result.

Code Instance
fs.readFile('file.txt', 'utf8', (err, data) => {
  if (err) console.error(err);
  else console.log(data);
});
12

💡What are process.nextTick() & setImmediate() in Node.js?

1️⃣ process.nextTick() - Executes before the next event loop. Great for immediate logic after a task, like error handling or cleanup.

2️⃣ setImmediate() - Executes after I/O events in the event loop. Useful for deferred tasks that shouldn’t block the current cycle.

Illustration
Illustration
13

🔍 What is Encoding?

1️⃣ Text Encoding: (UTF-8, ASCII) Converts characters into bytes so computers can store and display text.

2️⃣ Binary: Raw 0s & 1s, the language of computers.

3️⃣ Base64: Converts binary data( images, files) to text for safe web/email transmission.

4️⃣ URL Encoding: Handles special characters in URLs (%20 for spaces).

5️⃣ Hex: Easier-to-read format for binary (0-9, A-F).

14

💡 What is the Buffer class in Node.js?

It is a global object which handles raw binary data in memory (Space is allocated in RAM) allowing us to manipulate raw data streams efficiently

It is great for file I/O and Network communications

Example - Reading a binary file using buffer. fs.readFile reads binary data from file & saves in buffer. Buffer.toString(‘base64’) is used to convert raw binary data to base64

Code Instance
const fs = require('fs');

// Read a binary file (like an image) as a buffer
fs.readFile('image.png', (err, data) => {
  if (err) throw err;

  // `data` is a Buffer containing the binary content of the file
  console.log('Original Buffer:', data);

  // Convert the Buffer to a string in base64 encoding
  // Useful when we want to send it over a network
  const base64Data = data.toString('base64');
  console.log('Base64 Encoded Data:', base64Data);
});
15

🚀 What is Express.js and why is it required?

Its a minimal, flexible web framework for Node.js. It simplifies tasks like routing, handling requests/responses, and managing middleware.

It -> Reduces boilerplate code, Makes server-side dev fast & clean, Supports middleware, Simplifies Routing, Handles HTTP Methods, Serves Static Files, Supports Templating Engines and has Good Error Handling

Alternatives to Express - Koa.js, Fastify.js ( flexible & modern) , Hapi.js ( More config )

Code Instance
const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('Hello, Express!'));

app.listen(3000, () => console.log('Server running on port 3000'));
16

🙋 What is middleware in Express.js?

It is a function that executes during request-response cycle in express

Can use built-in, 3rd party or custom middlewares.

Used for tasks like logging, authentication, and parsing data.

Code Instance
app.use((req, res, next) => {
  console.log('Request Type:', req.method);
  next(); // Move to the next middleware or route handler
});
17

🧭 What is routing in Express.js?

It defines how an app responds to client requests at specific URL paths and HTTP methods (GET, POST, etc.). Its how you handle different actions (e.g. displaying data, creating resources) based on the URL.

How routing works in Express -

- Express supports all HTTP methods.

- Dynamic routes with parameters (e.g., /user/:id).

- Multiple handlers per route!

Code Instance
app.get('/users', (req, res) => {
  res.send('User list');
});

app.post('/users', (req, res) => {
  res.send('Create new user');
});
18

🧭 What are route parameters in Express.js?

Route parameters allow you to capture values from the URL. They are placeholders in the route definition (e.g., /user/:id).

Useful when u want to create dynamic routes that respond differently based on values in URL.

How route parameters work:

- Parameters are prefixed with : in the route path.

- You can access them using req.params inside your route handler.

Code Instance
app.get('/user/:id', (req, res) => {
  const userId = req.params.id;
  res.send(`User ID is ${userId}`);
});
19

❓What are query parameters in Express.js?

Query parameters are part of the URL that come after the question mark (?) and are used to send data as key-value pairs.

Can be accessed using req.query.

Example - /search?name=phone&category=electronics

Code Instance
const express = require('express');
const app = express();

app.get('/search', (req, res) => {
  const name = req.query.name;
  const category = req.query.category;
  res.send(`Searching for ${name} in the ${category} category`);
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
20

📋 Difference between res.send(), res.json(), and res.end() in Express.js?

- res.send(): It sends various types. Automatically detects and sets Content-Type header. It auto-ends response.

- res.json(): Sends JSON response with correct headers. It also sets Content-Type to application/json.

res.end(): Ends response without sending data (unless specified).