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 |
|---|---|
| एक shared 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 ची testing अधिक कठीण करते.
सविस्तर उत्तरांसह IT मुलाखत प्रश्नांचे ग्रंथालय — Junior पासून Senior पर्यंत.
देणगी द्या