Theory
box જે તમે ક્યારેય build નથી કર્યું
દરેક Unit 4 diagram માં box હતું labelled server, quietly answering search.php અને seats.json. તમે તેને drew કર્યું; તમે ક્યારેય build નથી કર્યું.
તેને traditionally build કરવાનો અર્થ હતો NEW language: PHP, Java, C#. બીજી syntax, બીજી toolchain, બીજું brain.
2009 માં, Ryan Dahl એ better question પૂછ્યું: browser already JavaScript ને brilliantly runs કરે છે: શા માટે SERVER નહીં? તેણે Chrome ના engine ને browser માંથી lift કર્યું, અને Node.js born થયું. તમારું jQuery-and-AJAX JavaScript હવે sides switch કરવાનું છે.
Theory
Node.js, ઔપચારિક રીતે
Node.js એ open-source, cross-platform JavaScript runtime environment છે જે Chrome ના V8 engine પર built છે જે JavaScript ને browser ની બહાર execute કરે છે.
દરેક phrase ને unpack કરો:
- runtime, language નહીં: language હજુ JavaScript છે; Node એ place છે જ્યાં તે runs કરે છે
- V8: Chrome નું engine, જે JS ને machine code માં compile કરે છે: Node તેની speed ને inherit કરે છે
- browser ની બહાર: કોઈ DOM નહીં, કોઈ window નહીં: instead, files, networks, ports: server territory
એક language, wire ની બંને sides: એ headline છે.
Theory
cashier જે ક્યારેય wait નથી કરતો
traditional server એ canteen છે એક customer માટે એક cashier સાથે: order biryani (slow to cook), અને તમારો cashier counter પર idle stands કરે છે જ્યાં સુધી તે ready ન થાય. crowds ને armies of cashiers ની જરૂર પડે છે.
Node એ એક brilliant cashier: તમારું order લે છે, તેને kitchen ને token સાથે hands કરે છે, અને next person ને immediately serve કરે છે. જ્યારે kitchen bell rings, તમારું order deliver થાય છે. એક thread, ક્યારેય waiting નહીં, thousands served: તે bell-and-token system એ event loop છે.
Theory
Features (exam list)
- Asynchronous અને event-driven: I/O ક્યારેય block નથી કરતું; callbacks fire થાય છે જ્યારે work complete થાય છે: fadeOut/AJAX model, હવે આખો server run કરે છે
- Single-threaded, highly scalable: એક event loop client દીઠ thread ને બદલે
- Very fast: V8 JavaScript ને machine code માં compile કરે છે
- No buffering: data chunks માં process થાય છે જેમ તે arrive થાય છે
- npm: world નું largest package ecosystem, Node સાથે installed
- Open source અને cross-platform: Windows, Linux, macOS
Follow along
Downloading અને first run (Windows)
- nodejs.org visit કરો બે buttons: LTS (Long Term Support) ને choose કરો, stable one જે most users માટે recommended છે.
- installer ને run કરો Next-next-finish; તે node AND npm ને install કરે છે, અને બંને ને PATH માં add કરે છે.
- command prompt માં verify કરો node -v Node version ને print કરે છે; npm -v npm નું print કરે છે. બંને answering = success.
- તમારી first file ને run કરો JavaScript ને app.js તરીકે save કરો, પછી: node app.js. loop માં ક્યાંય browser નહીં.
Practical
app.js: JavaScript જેની આસપાસ કોઈ browser નથી
// આ 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 શું અહીં MISSING છે: કોઈ document નહીં, કોઈ window નહીં, કોઈ DOM નહીં.
// Node તેમને files, networks અને ports સાથે trade કરે છે (next lessons).
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
Node.js exactly શું છે?
- નવું server-side programming language જે JavaScript ને similar છે
- JavaScript runtime જે Chrome ના V8 engine પર built છે જે JS ને browser ની બહાર run કરે છે
- developers માટે designed browser
- JavaScript library, jQuery જેવું, script tag સાથે included
Show the answer
JavaScript runtime જે Chrome ના V8 engine પર built છે જે JS ને browser ની બહાર run કરે છે
Node એ RUNTIME છે: environment જ્યાં same JavaScript language execute થાય છે, minus browser, plus server powers. Option A એ classic mislabel છે: કોઈ new language invent નથી થયું; તમારું Unit 2 અને 4 syntax untouched carry over થાય છે. Option C તેને inverts કરે છે: Node એ browser ને remove કર્યું ને બદલે being one. Option D delivery ને confuse કરે છે: libraries script tags દ્વારા pages માં load થાય છે; Node machine પર install થાય છે અને command line થી files ને run કરે છે: node app.js, ક્યાંય page નહીં.
Think first
એક thread, ten thousand students?
Fest results night: 10000 students server પર hit કરે છે એક સાથે. Node પાસે ONE thread છે. tap કરતા પહેલા: reason out કે શા માટે તે collapse નથી થતું, cashier ને use કરીને અને શું દરેક request mostly consist કરે છે.
Show the answer
કારણ કે web request એ mostly WAITING છે: file ને read કરવા માટે, database ને answer કરવા માટે, network packet ને crawl back કરવા માટે: અને Node ક્યારેય તેના thread ને waiting પર spend નથી કરતું. દરેક slow operation ને callback (kitchen token) સાથે hand off કરવામાં આવે છે; loop instantly next student ને serve કરે છે; completions events તરીકે ring back in થાય છે. ten thousand mostly-waiting requests એક never-idle thread માંથી beautifully interleave થાય છે. honest footnote: heavy CALCULATION (waiting નહીં) single thread ને block કરે છે: Node ની famous weakness, worth one exam line.
Watch out
First-week Node confusions
DOM ને Expect કરવું: document.getElementById અહીં exist નથી કરતું: કોઈ page loaded નથી. Node ના objects files, requests, responses છે.
node vs npm: node JavaScript ને RUNS કરે છે; npm packages ને INSTALLS કરે છે. બે tools, એક installer.
"Single-threaded એટલે weak": backwards I/O work માટે: event loop એ WHY Node scales: પણ calculation caveat ને full marks માટે write કરો.
Theory
async model તેની trilogy ને completes કરે છે
appearances ને count કરો: jQuery effects (animation ને start કરો, done થાય ત્યારે callback), AJAX (request ને send કરો, done થાય ત્યારે handler), અને હવે Node (I/O ને start કરો, done થાય ત્યારે callback). એક mental model, ત્રણ units: એકવાર master કરો, અને આ આખા subject ની hardest idea એ single idea છે. next lesson cashier shop ને open કરે છે: 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 કરવામાં આવે છે, ક્યારેય waited on નહીં.
- 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 સાથે run કરો.
- server પર કોઈ DOM નથી; heavy computation એ single thread ની true weakness છે.
- Memory hook: એક cashier, kitchen tokens, કોઈ wait નથી કરતું.