Node.js कैसे काम करता है
Node.js एक main thread plus एक helper system इस्तेमाल करता है ताकि यह freeze हुए बिना files और networks जैसी slow चीज़ों का wait कर सके।
In this page:
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
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. #}
आम गलतियां
- main thread पर लंबे CPU loops चलाना
- यह मान लेना कि Node I/O समेत सब कुछ के लिए एक thread इस्तेमाल करता है
- 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: