Theory
वह Word जो आपने 3 बार बिना Explain हुए Type किया
const http = require('http');
आपने अब तक यह line 2 lessons में faith पर इस्तेमाल की है। और इस बीच app.js चुपचाप बढ़ रही है: server code, seat arithmetic, जल्दी ही file logging: एक file सब कुछ collect कर रही है, exactly वही disease जिसे BCA403 के packages ने Java में cure किया था।
दोनों stories एक ही जगह ख़त्म होती हैं: Node का module system: कैसे code reusable files में split होता है, कैसे require() इसे fetch करता है, और कैसे एक file decide करती है यह दुनिया को क्या दिखाए।
Theory
Modules और require(), Formally
एक module related code की एक file है जिसकी एक controlled public surface होती है। require() एक load करता है और इसका public object return करता है।
दो species, argument की spelling से पहचानी जाती हैं:
- Built-in modules Node के अंदर ship होते हैं और bare name से load होते हैं:
require('http'), और friendsfs(files: अगला lesson),url(query strings),path,os - User-defined modules आपकी अपनी .js files हैं और relative path से load होती हैं:
require('./seats'):./का मतलब है यह folder, और यही Node को बताता है "मेरी file, package नहीं"
Practical
seats.js + app.js: बनाना और Include करना
// ---------- seats.js: a user-defined module ----------
exports.seatsLeft = function (booked, total) {
return total - booked; // PUBLIC: attached to exports
};
function auditLog(msg) { // PRIVATE: not exported,
// internal bookkeeping... // invisible outside this file
}
// ---------- app.js: including it ----------
// const seats = require('./seats'); // ./ = this folder
// console.log(seats.seatsLeft(310, 350)); // 40
// Simulated here so you can run the idea:
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 को serve किया जा सकता है; file का बाकी हर function back room में रहता है।
require('./seats') caller को exactly वही exports object देता है, तो seats.seatsLeft(310, 350) काम करता है और seats.auditLog(...) exist नहीं करता।
Idea पहचान रहे हैं? यह BCA403 का access control है फिर से जन्मा हुआ: exported = public members, unexported = private helpers: file scale पर encapsulation, ./seats import statement की भूमिका निभाते हुए।
Quiz
seats.js app.js के same folder में बैठी है, जो कहता है: const seats = require('seats'); क्या होता है?
- काम करता है: Node पहले current folder check करता है
- Error: Cannot find module 'seats': एक bare name Node को built-ins और installed packages में देखने भेजती है, आपकी files में नहीं
- काम करता है, पर सिर्फ़ exports पर attached functions ही load होते हैं
- Node interactively पूछता है आपका मतलब कौन सा seats module था
Show the answer
Error: Cannot find module 'seats': एक bare name Node को built-ins और installed packages में देखने भेजती है, आपकी files में नहीं
Spelling ही species HAI: एक bare name Node को इसकी built-in list और installed-packages folder में भेजता है, और वहाँ कोई 'seats' न मिलने पर, यह throw करता है: आपकी file कभी consult ही नहीं हुई। Fix 2 characters का है: require('./seats'): वह relative path जो कहता है मेरा folder, मेरी file। यह missing-dot-slash error single सबसे common first-week Node bug है, और exams इसे echo करते हैं। Option C एक true fact (सिर्फ़ exports load होते हैं) को ग़लत verdict में smuggle करता है: exports matter करने से पहले ही require fail हो जाता है।
Think first
Split Design कीजिए
FestConnect की server file bloat हो रही है: seat arithmetic, एक fee calculator, http server code। Module layout propose कीजिए: कौन सी files, हर एक क्या export करती है, और क्या private रहता है: फिर tap कीजिए।
Show the answer
तीन files। seats.js seatsLeft (और बाद में bookSeat) export करती है; इसके validation helpers private रहते हैं। fees.js feeFor(event) export करती है: अंदर price table private रहती है, तो prices बदलना एक ही file touch करता है। app.js कुछ export नहीं करती: यह entry point है, http, ./seats और ./fees require करते हुए, और उन्हें createServer में wire करते हुए। Principle, BCA403 के packages से identical: एक module एक topic, 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 करना भूल जाना: एक perfectly good function जो कभी exports पर attach नहीं हुआ call site पर undefined वापस आता है: जब एक required function missing हो तो पहले module की counter window check कीजिए।
.js Optional है: require('./seats') और require('./seats.js') दोनों काम करते हैं; convention इसे omit करता है।
Theory
एक Story की तीसरी Telling
अपने BCA में pattern के appearances count कीजिए: Java के packages + import + public (BCA403), VB.NET के Friend/Public surfaces (BCA404), और अब Node के modules + require + exports। अलग spelling, same law: related code group कीजिए, plumbing hide कीजिए, एक छोटी surface publish कीजिए। अगला lesson built-in url module को http के साथ इस्तेमाल करता है ताकि finally READ किया जा सके visitor ने क्या पूछा: 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।
- आपकी अपनी files relative path से load होती हैं: require('./seats'): ./ compulsory है।
- exports public counter है: attached functions बाहर callable हैं; बाकी सब private रहता है।
- Missing ./ Cannot find module देता है; missing exports undefined functions देता है।
- BCA403 packages जैसा ही organising law: छोटी public surface, hidden plumbing।
- Memory hook: shop-bought के लिए bare name, homemade के लिए dot-slash।