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.