Node.js uses a special model to handle many users at once without slowing down. What is this model called?
Think about how Node.js avoids waiting for tasks to finish before starting new ones.
Node.js uses an event-driven, non-blocking I/O model. This means it can start tasks and move on without waiting, allowing it to handle many connections efficiently.
Consider a Node.js server that uses a blocking function to read a large file. What is the effect on the server's ability to handle other requests?
Think about what blocking means: does it stop the whole server or just one part?
Blocking operations stop the single thread Node.js runs on, so the server cannot handle other requests until the operation finishes.
Which option correctly imports the fs module using ES modules syntax in Node.js?
ES modules use import keyword differently than CommonJS require.
In ES modules, you import the default export with import fs from 'fs';. The require syntax is for CommonJS modules.
Look at this code snippet:
const http = require('http');
http.createServer((req, res) => {
while(true) {}
res.end('Hello');
}).listen(3000);Why does the server freeze and not respond?
const http = require('http'); http.createServer((req, res) => { while(true) {} res.end('Hello'); }).listen(3000);
Think about what happens when code never finishes running inside the request handler.
The infinite loop blocks the single-threaded event loop, so the server cannot process any other events or send responses.
Consider this code snippet:
console.log('Start');
setTimeout(() => {
console.log('Timeout');
}, 0);
console.log('End');What is the order of the console output?
console.log('Start'); setTimeout(() => { console.log('Timeout'); }, 0); console.log('End');
Remember that setTimeout callbacks run after the current code finishes.
Even with 0 delay, setTimeout runs after the current synchronous code, so 'Start' and 'End' print first, then 'Timeout'.