Node.js केही थोरै design patterns मा निर्भर हुन्छ जुन यसको module system, async model, र streaming I/O सँग मिल्दछन्। प्रत्येक backend developer ले चिन्नुपर्ने सबैभन्दा सामान्य:
Node.js केही थोरै design patterns मा निर्भर हुन्छ जुन यसको module system, async model, र streaming I/O सँग मिल्दछन्। प्रत्येक backend developer ले चिन्नुपर्ने सबैभन्दा सामान्य:
module.exports / export मार्फत public API देखाउनुहुन्छ, र internals लाई private राख्नुहुन्छ। Node मा code organization को आधार।// db.js — हरेक `require("./db")` ले यही pool पाउँछ (module cache मार्फत singleton)।
const pool = createPool(process.env.DATABASE_URL);
module.exports = pool;
function createLogger(kind) {
return kind === "json" ? new JsonLogger() : new TextLogger();
}
EventEmitter, streams, र धेरैजसो core modules ले यो प्रयोग गर्छन्।const { EventEmitter } = require("node:events");
const bus = new EventEmitter();
bus.on("order:paid", (o) => sendReceipt(o));
bus.emit("order:paid", order);
(ctx, next) प्राप्त गर्छ, Express/Koa ले लोकप्रिय बनायो। cross-cutting concerns का लागि उत्कृष्ट (auth, logging, validation)।app.use((req, res, next) => { req.startedAt = Date.now(); next(); });
const { pipeline } = require("node:stream/promises");
await pipeline(fs.createReadStream("in"), gzip(), fs.createWriteStream("out.gz"));
| आवश्यकता | Pattern |
|---|---|
| एउटा साझा resource | Singleton (module cache) |
objects बनाउने, new लुकाउने | Factory |
| async events मा प्रतिक्रिया दिने | Observer / EventEmitter |
| cross-cutting request logic | Middleware |
| ठूलो data सुरक्षित रूपमा process गर्ने | Streams / pipeline |
| testable, decoupled dependencies | Dependency Injection |
Patterns एउटा साझा शब्दावली हो: "config एउटा module-cached singleton हो" वा "auth एउटा middleware हो" भन्नुले केही शब्दमै एउटा design सम्प्रेषण गर्छ। Node को आफ्नै core (EventEmitter, streams, module system) यिनैबाट बनेको छ, त्यसैले तिनलाई चिन्नुले तपाईंलाई frameworks पढ्न र आफ्नै services संरचना गर्न मद्दत गर्छ। अन्तर्वार्ताकारहरूले यो प्रश्न प्रयोग गर्छन् कि तपाईं callbacks लाई मनपरी जोड्ने मात्र होइन, तर मिल्दो pattern तर्फ पुग्नुहुन्छ — र यसको trade-offs थाहा पाउनुहुन्छ, जस्तै singleton ले global state लाई test गर्न गाह्रो बनाउँछ।
विस्तृत उत्तरसहित IT अन्तर्वार्ता प्रश्नहरूको पुस्तकालय — जुनियरदेखि सिनियरसम्म।
दान गर्नुहोस्