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 બહાર પાડો છો, અને આંતરિક ભાગ 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) મેળવે છે, 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 ને સુરક્ષિત રીતે પ્રોસેસ કરો | Streams / pipeline |
| testable, decoupled dependencies | Dependency Injection |
Patterns એ સહિયારી શબ્દાવલી છે: "config એ module-cached singleton છે" કે "auth એ middleware છે" કહેવું થોડા શબ્દોમાં design પહોંચાડે છે. Node નું પોતાનું core (EventEmitter, streams, module system) આ patterns માંથી બનેલું છે, તેથી તેમને ઓળખવાથી તમને frameworks વાંચવામાં અને તમારી પોતાની services ને રચવામાં મદદ મળે છે. ઇન્ટરવ્યુઅર્સ આ પ્રશ્નનો ઉપયોગ એ ચકાસવા કરે છે કે તમે માત્ર callbacks ને આડેધડ જોડતા નથી, પરંતુ બંધબેસતું pattern પસંદ કરો છો — અને તેના trade-offs જાણો છો, જેમ કે singleton global state ને test કરવું અઘરું બનાવે છે.
વિગતવાર જવાબો સાથે IT ઇન્ટરવ્યૂ પ્રશ્નોની લાઇબ્રેરી — જુનિયરથી સિનિયર સુધી.
દાન કરો