0% found this document useful (0 votes)
31 views4 pages

Nodejs Interview Questions

The document lists the top 50 Node.js interview questions along with precise answers and examples. Key topics include the Node.js runtime environment, asynchronous programming concepts, modules, and Express.js framework. It also covers error handling, middleware, and security practices in Node.js applications.

Uploaded by

davidanuj2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

Nodejs Interview Questions

The document lists the top 50 Node.js interview questions along with precise answers and examples. Key topics include the Node.js runtime environment, asynchronous programming concepts, modules, and Express.js framework. It also covers error handling, middleware, and security practices in Node.js applications.

Uploaded by

davidanuj2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Top 50 Node.

js Interview Questions with Precise Answers and Examples

1. What is Node.js?\ Node.js is a runtime environment that allows executing JavaScript outside the browser.
Built on Chrome's V8 engine, it is designed for building scalable, fast, and non-blocking server-side
applications.\ Example: Running a server with Node.js.

console.log('Node.js is running');

2. Is Node.js single-threaded?\ Yes, Node.js is single-threaded for event handling but uses a thread pool
via libuv to manage asynchronous tasks like file operations or DNS lookups.

3. Difference between Node.js and JavaScript?\ JavaScript is a language; Node.js is a runtime that lets you
run JavaScript on the server-side.

4. Why use Node.js?\ It provides event-driven, non-blocking architecture ideal for real-time apps, APIs, and
scalable network systems.

5. What is V8 engine?\ It is Google's open-source JavaScript engine that compiles JavaScript directly to
machine code, making execution fast.

6. What is the Event Loop in Node.js?\ The Event Loop handles asynchronous operations in Node.js,
allowing non-blocking execution by offloading tasks and executing callbacks.

7. Difference between process.nextTick() and setImmediate()?

• process.nextTick() executes before the next event loop iteration.


• setImmediate() runs after the current poll phase of the event loop.

8. What is non-blocking I/O?\ Non-blocking I/O allows the application to continue running other tasks
while waiting for I/O operations to complete.

9. What is callback hell?\ Deeply nested callbacks that make code unreadable and hard to maintain.
Avoided using Promises or async/await.

10. How to avoid callback hell?\ Using Promises or async/await to flatten the structure.

async function fetchData() {


let data = await getData();
}

11. What is a module in Node.js?\ A module is a reusable block of code exported using module.exports
and imported using require() .

1
12. What is NPM?\ Node Package Manager, used to manage Node.js packages and project dependencies.

13. Difference between require() and import?\ require() is CommonJS syntax; import is ES6 syntax,
typically used in modern projects or with transpilers.

14. What is package.json?\ A JSON file storing project metadata, dependencies, and scripts.

15. How to create a module?

// math.js
module.exports = { add: (a, b) => a + b };

16. What is EventEmitter?\ A class allowing objects to emit and listen for events.

const EventEmitter = require('events');


const emitter = new EventEmitter();
emitter.on('start', () => console.log('Started'));
emitter.emit('start');

17. What are streams in Node.js?\ Streams handle data in chunks, improving efficiency for large data.
Types: Readable, Writable, Duplex, Transform.

18. What is buffer in Node.js?\ A temporary memory area for storing binary data, used with streams.

19. How to handle uncaught exceptions?

process.on('uncaughtException', (err) => console.error(err));

20. Difference between readFile and createReadStream?\ readFile loads entire file in memory;
createReadStream reads data in chunks.

21. How to read a file asynchronously?

const fs = require('fs');
fs.readFile('test.txt', 'utf8', (err, data) => console.log(data));

22. How to create an HTTP server?

const http = require('http');


http.createServer((req, res) => res.end('Hello')).listen(3000);

2
23. What is middleware?\ Functions that execute during the request-response cycle, widely used in
Express.js.

24. What is cluster module?\ Allows creating multiple Node.js processes to utilize multi-core CPUs.

25. What is non-blocking code?\ Code that doesn't block the main thread; other operations continue while
tasks complete in the background.

26. What are Promises?\ Objects representing eventual completion/failure of asynchronous operations.

27. What is async/await?\ Syntax simplifying asynchronous code by allowing await inside async
functions.

28. Difference between synchronous and asynchronous code?

• Synchronous blocks code execution.


• Asynchronous allows tasks to run in parallel.

29. What is process in Node.js?\ An object representing the running Node.js process, giving control and
information.

30. What is REPL?\ Read-Eval-Print Loop, an interactive shell for Node.js.

31. What is Express.js?\ A lightweight Node.js framework for building web servers and APIs.

32. How to install Express?

npm install express

33. How to create a basic Express server?

const express = require('express');


const app = express();
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);

34. What is routing in Express?\ Defining URL paths to handle client requests.

35. What are middlewares in Express?\ Functions with access to request, response, and next middleware.

36. How to prevent callback hell?\ Use Promises, async/await, or modular code structure.

37. How to handle errors properly?\ Using try-catch , centralized error handlers, and .catch() with
Promises.

3
38. How to secure a Node.js app?\ Input validation, Helmet.js for headers, rate limiting, and avoiding
injection attacks.

39. What is Helmet in Node.js?\ Middleware that secures HTTP headers.

40. What is CORS?\ Cross-Origin Resource Sharing, allows or restricts resource access from different
domains.

41. How to enable CORS in Express?

const cors = require('cors');


app.use(cors());

42. What is the difference between process.exit() and process.kill()?\ process.exit() terminates the
current process; process.kill() sends a signal to terminate.

43. How to debug Node.js?\ Using console logs, Node Inspector, Chrome DevTools, or VSCode debugger.

44. What is load balancing in Node.js?\ Distributing traffic across multiple processes for better scalability.

45. What is spawn() vs exec()?

• spawn() streams large outputs.


• exec() buffers output, good for small tasks.

46. What is middleware chaining?\ Multiple middleware functions executed in sequence.

47. What is difference between fork and cluster?\ fork() creates child processes; cluster uses
fork() to create multiple server instances.

48. What is process.env?\ Object storing environment variables.

49. How to increase max listeners in EventEmitter?

emitter.setMaxListeners(20);

50. How to handle unhandled Promise rejections?

process.on('unhandledRejection', (err) => console.error(err));

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy