Concepts, working and Features; downloading Node.js

Node.js Chrome के V8 engine पर browser के बाहर JavaScript चलाता है: single-threaded, event-driven, non-blocking, और nodejs.org से 5 minutes में installed।

10 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

वह Box जो आपने कभी नहीं बनाया

हर Unit 4 diagram में एक box था जिस पर लिखा था server, चुपचाप search.php और seats.json को answer करते हुए। आपने इसे draw किया; कभी बनाया नहीं।

इसे traditionally बनाने का मतलब था एक NEW language: PHP, Java, C#। एक second syntax, second toolchain, second brain।

2009 में, Ryan Dahl ने बेहतर सवाल पूछा: browser पहले से JavaScript brilliantly चलाता है: SERVER क्यों नहीं चल सकता? उन्होंने Chrome का engine browser से बाहर उठाया, और Node.js का जन्म हुआ। आपका jQuery-and-AJAX JavaScript अब sides switch करने वाला है।

Theory

Node.js, Formally

Node.js एक open-source, cross-platform JavaScript runtime environment है जो Chrome के V8 engine पर built है जो JavaScript को browser के OUTSIDE execute करता है।

हर phrase को unpack कीजिए:

  • runtime, language नहीं: language अभी भी JavaScript है; Node वह जगह है जहाँ यह चलता है
  • V8: Chrome का engine, जो JS को machine code में compile करता है: Node इसकी speed inherit करता है
  • browser के outside: कोई DOM नहीं, कोई window नहीं: इसके बजाय, files, networks, ports: server territory

एक language, wire के दोनों sides: यही headline है।

Theory

वह Cashier जो कभी Wait नहीं करता

एक traditional server एक canteen है जिसमें per customer एक cashier होता है: biryani order कीजिए (cook होने में slow), और आपका cashier counter पर idle खड़ा रहता है जब तक यह ready न हो। Crowds को cashiers की armies चाहिए।

Node है एक brilliant cashier: आपका order लेता है, इसे एक token के साथ kitchen को hand करता है, और तुरंत अगले person को serve करता है। जब kitchen bell बजती है, आपका order deliver होता है। एक thread, कभी wait नहीं करता, हज़ारों serve होते हैं: वही bell-and-token system event loop है।

Theory

Features (Exam List)

  • Asynchronous और event-driven: I/O कभी block नहीं होता; काम पूरा होने पर callbacks fire होते हैं: fadeOut/AJAX model, अब पूरे server को चलाते हुए
  • Single-threaded, highly scalable: per client एक thread के बजाय एक event loop
  • बहुत Fast: V8 JavaScript को machine code में compile करता है
  • No Buffering: data आते ही chunks में process होता है
  • npm: दुनिया का सबसे बड़ा package ecosystem, Node के साथ installed
  • Open Source और Cross-Platform: Windows, Linux, macOS

Follow along

Downloading और First Run (Windows)

  1. nodejs.org Visit कीजिए दो buttons: LTS (Long Term Support) चुनिए, वह stable version जो ज़्यादातर users के लिए recommend होता है।
  2. Installer चलाइए Next-next-finish; यह node AND npm install करता है, और दोनों को आपके PATH में add करता है।
  3. एक Command Prompt में Verify कीजिए node -v Node की version print करता है; npm -v npm की। दोनों का answer देना = success।
  4. अपनी पहली File चलाइए JavaScript को app.js की तरह save कीजिए, फिर: node app.js। Loop में कहीं भी कोई browser नहीं।

Practical

app.js: बिना Browser के JavaScript

// This is server-side JavaScript: same language, new home.
console.log("FestConnect server starting...");

const total = 350;
const booked = 310;
console.log("Seats left for Garba Night: " + (total - booked));

// Note what is MISSING here: no document, no window, no DOM.
// Node trades those for files, networks and ports (next lessons).

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

Node.js exactly क्या है?

  1. JavaScript जैसी एक नई server-side programming language
  2. Chrome के V8 engine पर built एक JavaScript runtime जो browser के बाहर JS चलाता है
  3. Developers के लिए design किया गया एक browser
  4. jQuery जैसी एक JavaScript library, एक script tag के साथ included
Show the answer

Chrome के V8 engine पर built एक JavaScript runtime जो browser के बाहर JS चलाता है

Node एक RUNTIME है: एक environment जहाँ वही JavaScript language execute होती है, browser minus, server powers plus। Option A classic mislabel है: कोई नई language invent नहीं हुई; आपकी Unit 2 और 4 की syntax बिना छुए carry over होती है। Option C इसे invert करता है: Node ने browser को remove किया, इसकी बजाय एक browser होने के। Option D delivery को confuse करता है: libraries pages में script tags के ज़रिए LOAD होती हैं; Node एक machine पर installed होता है और command line से files चलाता है: node app.js, कहीं भी कोई page नहीं।

Think first

एक Thread, दस हज़ार Students?

Fest results night: 10000 students एक साथ server hit करते हैं। Node के पास ONE thread है। Tap करने से पहले: cashier और हर request असल में मुख्यतः किस चीज़ की बनी होती है, यह इस्तेमाल करते हुए reason कीजिए यह collapse क्यों नहीं होता।

Show the answer

क्योंकि एक web request मुख्यतः WAITING है: एक file पढ़ने के लिए, एक database answer देने के लिए, एक network packet crawl करके वापस आने के लिए: और Node कभी अपना thread waiting पर spend नहीं करता। हर slow operation एक callback (kitchen token) के साथ hand off होता है; loop तुरंत अगले student को serve करता है; completions events की तरह वापस ring करते हैं। दस हज़ार mostly-waiting requests एक कभी-idle-न-होने वाले thread के through खूबसूरती से interleave होते हैं। Honest footnote: heavy CALCULATION (waiting नहीं) single thread को block करता है: Node की famous weakness, एक exam line लायक।

Watch out

First-Week Node Confusions

DOM Expect करना: document.getElementById यहाँ exist नहीं करता: कोई page load नहीं हुआ। Node के objects files, requests, responses हैं।

node बनाम npm: node JavaScript RUN करता है; npm packages INSTALL करता है। दो tools, एक installer।

"Single-threaded का मतलब Weak है": I/O work के लिए backwards: event loop WHY Node scale करता है इसकी वजह है: पर full marks के लिए calculation caveat लिखिए।

Theory

Async Model अपनी Trilogy Complete करता है

Pattern के appearances count कीजिए: jQuery effects (animation start कीजिए, done होने पर callback), AJAX (request send कीजिए, done होने पर handler), और अब Node (I/O start कीजिए, done होने पर callback)। एक mental model, तीन units: इसे एक बार master कीजिए, और इस पूरे subject का सबसे hard idea एक single idea है। अगला lesson cashier shop खोलता है: http.createServer, और localhost:8080 hello कहता है।

Summary

Key takeaways

  • Node.js = open-source, cross-platform JavaScript RUNTIME, Chrome के V8 पर: browser के बाहर JS।
  • एक language, दोनों sides: browser JS और server JS।
  • Working: single thread + event loop; I/O callbacks के साथ hand off होता है, कभी wait नहीं होता।
  • Features: async/event-driven, scalable single thread, V8 speed, no buffering, npm, cross-platform।
  • Install: nodejs.org LTS, node -v और npm -v से verify कीजिए, node app.js से चलाइए।
  • Server पर कोई DOM नहीं; heavy computation single thread की असली weakness है।
  • Memory hook: एक cashier, kitchen tokens, कोई wait नहीं करता।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Node.js

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati