express-partial-response is an Express.js middleware designed to filter parts of JSON responses based on a `fields` query-string parameter. It mimics the "Partial Response" feature found in Google APIs, allowing clients to request only a subset of data from an API endpoint, thereby reducing bandwidth and processing on the client side. The current stable version is 1.0.4. The project appears to be in a maintenance state, with no recent major updates mentioned, suggesting a stable and complete feature set rather than active development. Its core functionality is powered by the `json-mask` library, which handles the actual JSON object filtering according to the specified field syntax. This middleware differentiates itself by providing a direct, out-of-the-box integration for Express applications, simplifying the implementation of partial responses without requiring manual integration of `json-mask` into every route.
npm install express-partial-responseVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `express-partial-response` middleware with an Express application, creating two example routes (`/users/:id` and `/products`) that return JSON data. It shows how the middleware automatically filters the response based on the `?fields=` query parameter, illustrating various `json-mask` syntax examples with `curl` commands.
Consult the `json-mask` library documentation (e.g., `https://github.com/nemtsov/json-mask#syntax`) for the correct and full syntax of the `fields` parameter.
Ensure `express-partial-response` is only used on routes that are expected to respond with JSON data. Consider applying it selectively using route-specific middleware if some routes serve non-JSON content.
Implement robust server-side authorization and data serialization logic to explicitly omit sensitive fields before they reach the `res.json()` call, or before `express-partial-response` processes them. Do not rely solely on client-requested field masks for security.
If working in a pure ESM project, you might need to use dynamic `import()` or configure your bundler to handle CJS modules. Alternatively, ensure your project is configured for CommonJS or use `require()` syntax.
Call the `partialResponse` function to get the actual middleware. Correct usage is `app.use(partialResponse());` or `app.use(partialResponse({ options }));`.Double-check the `fields` query parameter against the `json-mask` syntax documentation. Common mistakes include typos, incorrect nesting parentheses, or missing commas.
This package is CJS-only. You must either use a CommonJS environment, or if you must use ESM, use dynamic `import()` (e.g., `const partialResponse = await import('express-partial-response'); const middleware = partialResponse.default();`) or a build tool that handles CJS-to-ESM conversion.