What is a Proxy in Node.js and how is it used?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized Node.js deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
A Proxy in JavaScript (and therefore Node.js) is an ES6 object that wraps another object (the target) and intercepts operations on it using handler functions called traps. The Proxy constructor takes a target object and a handler: const proxy = new Proxy(target, handler);. Common traps: get(target, prop) — intercepts property access; set(target, prop, value) — intercepts property assignment; has(target, prop) — intercepts in operator; apply(target, thisArg, args) — intercepts function calls; construct(target, args) — intercepts new. Use cases in Node.js: (1) Validation: validate property assignments automatically; (2) Lazy loading: defer expensive object initialization until first access; (3) Mocking in tests: intercept method calls without modifying the original object; (4) Observable objects: track changes to state (used internally by Vue 3's reactivity); (5) API client generation: generate method calls dynamically from property access patterns; (6) Logging/debugging: log all property accesses. Reflect API complements Proxy by providing default implementations of each trap.
Pro Tip
This topic has Node.js-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
Previous
What is the difference between Fastify and Express?
Next
What is Domain-Driven Design (DDD) and how is it applied in Node.js?
More Node.js Questions
View all →- Advanced How does Node.js handle concurrency without multiple threads?
- Advanced What is the Node.js memory model and how does garbage collection work?
- Advanced What are memory leaks in Node.js and how do you detect them?
- Advanced What is the difference between process.exit() and throwing an uncaught exception?
- Advanced What is the N+1 query problem and how do you solve it in Node.js?