← Back to Node.js Course | Chapter 1: Introduction | Lesson 2 of 7

Node.js कैसे काम करता है

Node.js एक main thread plus एक helper system इस्तेमाल करता है ताकि यह freeze हुए बिना files और networks जैसी slow चीज़ों का wait कर सके।

Node.js कैसे काम करता है

Node.js आपका JavaScript एक single thread पर चलाता है, लेकिन slow I/O work (files, network, timers) libuv के जरिए operating system और एक thread pool को handover करता है।

काम खत्म होने पर, एक callback queue होता है और main thread पर चलता है। यह non-blocking model एक process को हज़ारों connections serve करने देता है।

Note: CPU-heavy work single thread block करता है; इसे worker threads या किसी दूसरे process में move करें।

उदाहरण: How Node.js works

javascript
console.log("1. start");

setTimeout(() => console.log("3. timer callback (after I/O style wait)"), 0);

console.log("2. end of script");

// Output:
// 1. start
// 2. end of script
// 3. timer callback (after I/O style wait)

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. main thread पर लंबे CPU loops चलाना
  2. यह मान लेना कि Node I/O समेत सब कुछ के लिए एक thread इस्तेमाल करता है
  3. servers में synchronous file APIs से event loop block करना
चैप्टर सारांश
  • JavaScript एक main thread पर चलता है
  • libuv async I/O handle करता है
  • काम complete होने पर Callbacks चलते हैं
  • main thread block करने से बचें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.