Fullstack Backend Interview Q&A: Node.js, Express.
js, MySQL, MongoDB
Comprehensive Backend Interview Questions & Answers
Covering Node.js, Express.js, MySQL, MongoDB
Prepared for: Satyanarayan Patra
Compiled by: ChatGPT
Date: June 2025
Page 1
Fullstack Backend Interview Q&A: Node.js, Express.js, MySQL, MongoDB
1. What is the difference between process.nextTick(), setImmediate(), and setTimeout() in Node.js?
In Node.js, the event loop handles asynchronous operations. These three methods queue functions
differently:
- `process.nextTick()` queues the callback to be invoked after the current operation completes but before the
event loop continues. It's part of the microtask queue.
- `setImmediate()` queues the callback to be executed in the next iteration of the event loop (macrotask
queue).
- `setTimeout(fn, 0)` schedules the callback after a minimum delay of 0 ms, meaning it will be put in the timer
queue and executed after the timer phase is reached.
Use `nextTick` for code that must be executed immediately after the current function, and `setImmediate` for
deferring execution until the next loop iteration.
2. How do you prevent SQL Injection in a Node.js MySQL application?
To prevent SQL injection:
- Use parameterized queries or prepared statements provided by libraries like `mysql2` or `sequelize`.
- Avoid directly concatenating user inputs into SQL queries.
- Use ORM frameworks that auto-sanitize inputs.
Example with `mysql2`:
```js
const mysql = require('mysql2');
const connection = mysql.createConnection({host: 'localhost', user: 'root', database: 'test'});
const username = req.body.username;
connection.query('SELECT * FROM users WHERE username = ?', [username], function (err, results) {
// Safe query
});
```
Page 2