Theory
word જે તમે 3 times unexplained typed કર્યું છે
const http = require('http');
તમે આ line ને હવે 2 lessons માં faith પર use કર્યું છે. અને meantime app.js quietly grow થઈ રહ્યું છે: server code, seat arithmetic, soon file logging: એક file everything ને collect કરે છે, exact disease જે BCA403 ના packages એ Java માં cure કર્યું હતું.
બંને stories same place માં end થાય છે: Node નું module system: code કેવી રીતે reusable files માં split થાય છે, require() તેમને કેવી રીતે fetch કરે છે, અને file કેવી રીતે decide કરે છે કે તે world ને શું show કરે છે.
Theory
Modules અને require(), ઔપચારિક રીતે
module એ related code ની file છે controlled public surface સાથે. require() એક ને load કરે છે અને તેનું public object return કરે છે.
બે species, argument ની spelling દ્વારા told apart:
- Built-in modules Node ની અંદર ship થાય છે અને bare name દ્વારા load થાય છે:
require('http'), અને friendsfs(files: next lesson),url(query strings),path,os - User-defined modules એ તમારા own .js files છે અને relative path દ્વારા load થાય છે:
require('./seats'):./નો અર્થ છે this folder, અને તે એ છે જે Node ને કહે છે "my file, not a package"
Practical
seats.js + app.js: create અને include
// ---------- seats.js: a user-defined module ----------
exports.seatsLeft = function (booked, total) {
return total - booked; // PUBLIC: exports સાથે attached
};
function auditLog(msg) { // PRIVATE: exported નહીં,
// internal bookkeeping... // આ file ની બહાર invisible
}
// ---------- app.js: તેને including ----------
// const seats = require('./seats'); // ./ = this folder
// console.log(seats.seatsLeft(310, 350)); // 40
// અહીં simulated જેથી તમે idea ને run કરી શકો:
const seats = { seatsLeft: (booked, total) => total - booked };
console.log("Seats left: " + seats.seatsLeft(310, 350)); // 40
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
exports: module નું public counter
module ની અંદર, exports object એ counter window છે: જે કંઈ તમે તેના સાથે attach કરો છો (exports.seatsLeft = ...) તે customers ને servable છે; file નો દરેક other function back room માં stays કરે છે.
require('./seats') caller ને exactly એ exports object ને hands કરે છે, એટલે seats.seatsLeft(310, 350) work કરે છે અને seats.auditLog(...) exist નથી કરતું.
idea ને recognise કરો? તે BCA403 નું access control છે જે reborn થયું છે: exported = public members, unexported = private helpers: encapsulation file scale પર, ./seats સાથે import statement ને play કરતું.
Quiz
seats.js app.js ની same folder માં sits કરે છે, જે કહે છે: const seats = require('seats'); શું થાય છે?
- Works: Node current folder ને પહેલા check કરે છે
- Error: Cannot find module 'seats': bare name Node ને built-ins અને installed packages ને look કરવા મોકલે છે, તમારા files નહીં
- Works, પણ ફક્ત functions જે exports સાથે attached છે તે load થાય છે
- Node interactively પૂછે છે કે તમે કયા seats module ને meant કર્યું
Show the answer
Error: Cannot find module 'seats': bare name Node ને built-ins અને installed packages ને look કરવા મોકલે છે, તમારા files નહીં
spelling IS species છે: bare name Node ને તેની built-in list અને installed-packages folder પર send કરે છે, અને ત્યાં કોઈ 'seats' ના find કરવાથી, તે throw કરે છે: તમારી file ક્યારેય consulted નહોતી. fix એ 2 characters છે: require('./seats'): relative path જે કહે છે my folder, my file. આ missing-dot-slash error એ single most common first-week Node bug છે, અને exams તેને echo કરે છે. Option C true fact ને smuggle કરે છે (ફક્ત exports load થાય છે) wrong verdict માં: require fail થાય છે exports matter કરે તે પહેલાં.
Think first
split ને design કરો
FestConnect નું server file bloating થઈ રહ્યું છે: seat arithmetic, fee calculator, http server code. module layout ને propose કરો: કયા files, દરેક શું exports કરે છે, અને શું private રહે છે: પછી tap કરો.
Show the answer
ત્રણ files. seats.js seatsLeft ને exports કરે છે (અને later bookSeat); તેના validation helpers private રહે છે. fees.js feeFor(event) ને exports કરે છે: price table અંદર private રહે છે, એટલે prices ને change કરવા એક file ને touch કરે છે. app.js કંઈ export નથી કરતું: તે entry point છે, http, ./seats અને ./fees ને requiring, અને તેમને createServer માં wiring કરે છે. principle, BCA403 ના packages સાથે identical છે: એક topic per module, smallest possible public surface, અને entry file thin conductor તરીકે. એ layout answer, reasons સાથે, full marks છે.
Watch out
Module slips
missing ./: quiz bug; built-ins અને packages ને name દ્વારા, તમારા FILES ને path દ્વારા.
export કરવાનું Forget કરવું: perfectly good function જે ક્યારેય exports સાથે attached નહોતું તે call site પર undefined back આવે છે: જ્યારે require'd function missing હોય ત્યારે module નું counter window પહેલા check કરો.
.js optional છે: require('./seats') અને require('./seats.js') બંને work કરે છે; convention તેને omit કરે છે.
Theory
એક story નું third telling
pattern ના appearances ને count કરો તમારા BCA માં: Java ના packages + import + public (BCA403), VB.NET ના Friend/Public surfaces (BCA404), અને હવે Node ના modules + require + exports. different spelling, same law: related code ને group કરો, plumbing ને hide કરો, small surface ને publish કરો. next lesson built-in url module ને http સાથે use કરે છે finally visitor એ શું asked તેને READ કરવા: query strings, split અને served.
Summary
Key takeaways
- module એ reusable code file છે; require() તેને load કરે છે અને તેનું public object return કરે છે.
- Built-ins bare name દ્વારા load થાય છે: http, fs, url, path, os.
- તમારા own files relative path દ્વારા load થાય છે: require('./seats'): ./ compulsory છે.
- exports એ public counter છે: attached functions બહાર callable છે; બાકી નું private રહે છે.
- Missing ./ આપે છે Cannot find module; missing exports આપે છે undefined functions.
- Same organising law જે BCA403 packages: small public surface, hidden plumbing.
- Memory hook: bare name shop-bought માટે, dot-slash homemade માટે.