Node.js తన module system, async model మరియు streaming I/Oకు సరిపోయే కొన్ని design patternsపై ఆధారపడుతుంది. ప్రతి backend developer గుర్తించవలసిన అత్యంత సాధారణమైనవి:
Node.js తన module system, async model మరియు streaming I/Oకు సరిపోయే కొన్ని design patternsపై ఆధారపడుతుంది. ప్రతి backend developer గుర్తించవలసిన అత్యంత సాధారణమైనవి:
module.exports / export ద్వారా public APIను బహిర్గతం చేస్తారు, అంతర్గత భాగాలను privateగా ఉంచుతారు. Nodeలో code నిర్వహణకు పునాది.// 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)ను స్వీకరించే functionsల pipeline, Express/Koa ద్వారా ప్రాచుర్యం పొందింది. క్రాస్-కటింగ్ 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 |
| క్రాస్-కటింగ్ request logic | Middleware |
| పెద్ద dataను సురక్షితంగా ప్రాసెస్ చేయి | Streams / pipeline |
| Testable, decoupled dependencies | Dependency Injection |
Patterns ఒక భాగస్వామ్య పదజాలం: "config అనేది module-cache చేయబడిన singleton" లేదా "auth అనేది middleware" అని చెప్పడం ఒక designను కొన్ని పదాల్లో తెలియజేస్తుంది. Node యొక్క సొంత core (EventEmitter, streams, module system) వీటి నుండి నిర్మించబడింది, కాబట్టి వాటిని గుర్తించడం frameworksను చదవడానికి మరియు మీ సొంత servicesను నిర్మించడానికి సహాయపడుతుంది. మీరు కేవలం callbacksను యాదృచ్ఛికంగా వైర్ చేయడం కాకుండా, సరిపోయే patternను చేరుకుంటారా — మరియు singleton global stateను test చేయడం కష్టతరం చేయడం వంటి దాని trade-offsను తెలుసుకుంటారా అని తనిఖీ చేయడానికి interviewers ఈ ప్రశ్నను ఉపయోగిస్తారు.
జూనియర్ నుండి సీనియర్ వరకు వివరణాత్మక సమాధానాలతో IT ఇంటర్వ్యూ ప్రశ్నల లైబ్రరీ.
విరాళం