https://github.com/nemtsov/express-partial-response
Express middleware for filtering-out parts of JSON responses based on the `fields` query-string; à la Google API's Partial Response.
https://github.com/nemtsov/express-partial-response
Last synced: 9 months ago
JSON representation
Express middleware for filtering-out parts of JSON responses based on the `fields` query-string; à la Google API's Partial Response.
- Host: GitHub
- URL: https://github.com/nemtsov/express-partial-response
- Owner: nemtsov
- License: mit
- Created: 2013-07-15T05:30:33.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2021-07-07T10:14:20.000Z (over 4 years ago)
- Last Synced: 2025-03-30T18:09:57.220Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 111
- Watchers: 4
- Forks: 15
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-express - express-partial-response - Express 中间件模块,使用 Google API 的 Partial Response,根据 fields 查询字符串过滤掉 JSON 响应的各个部分。 (中间件)
README
# Express Partial Response Middleware [](http://badge.fury.io/js/express-partial-response)
This Express Middleware will allow you to send a subset of a JSON object
instead of the entire object from your HTTP services. To do so, your services
will begin accepting the `?fields=` query-string that, using a simple language,
will specify which fields and sub-fields to keep and which to ignore.
If you've used the Google APIs, provided a `?fields=` query-string to get a
[Partial Response](https://developers.google.com/+/api/#partial-responses),
and wanted to do the same for your own server, now you can do so with this
middleware.
_Underneath, this middleware uses [json-mask](https://github.com/nemtsov/json-mask).
Use it directly without this middleware if you need more flexibility._
# Installation
```
npm install express-partial-response
```
# Usage
```js
const express = require('express');
const partialResponse = require('express-partial-response');
const app = express();
app.use(partialResponse());
app.get('/', (req, res) => {
res.json({
firstName: 'Mohandas',
lastName: 'Gandhi',
aliases: [
{
firstName: 'Mahatma',
lastName: 'Gandhi'
},
{
firstName: 'Bapu'
}
]
});
});
app.listen(4000);
```
Let's test it:
```
$ curl 'http://localhost:4000'
{"firstName":"Mohandas","lastName":"Gandhi","aliases":[{"firstName":"Mahatma","lastName":"Gandhi"},{"firstName":"Bapu"}]}
$ # Let's just get the first name
$ curl 'http://localhost:4000?fields=lastName'
{"lastName":"Gandhi"}
$ # Now, let's just get the first names directly as well as from aliases
$ curl 'http://localhost:4000?fields=firstName,aliases(firstName)'
{"firstName":"Mohandas","aliases":[{"firstName":"Mahatma"},{"firstName":"Bapu"}]}
```
**Note:** take a look at `/example`.
# Syntax
Look at [json-mask](https://github.com/nemtsov/json-mask) for the available syntax of the `fields` param.
# Options
`query` specifies the query-string to use. Defaults to `fields`
```js
app.use(
partialResponse({
query: 'filter'
})
);
```
# License
MIT. See LICENSE