Comments (6)
Hi @typicode
we can enable this (a count property in the body response) via a flag?
Several (request) solution doesn't provide a way to access to header data.
from json-server.
I found a solution by creating server.js :
Const jsonServer = require('json-server')
.....
....
....router.render = function(req, res) {
Var newData = {
TotalCount: res.get('X-Total-Count'),
Result: res.locals.data
}
res.json(newData)
}
@lowdev, Thank you, This should be the accepted answer.
I was searching for how to get X-Total-Count from render, your answer inspired me. Here is some slightly formatted code.
router.render = (req, res) => {
// if it's a GET request and result is an Array
if (req.method === 'GET' && Array.isArray(res.locals.data)) {
res.json({
total: res.get('X-Total-Count'),
list: res.locals.data,
})
}
}
from json-server.
I found a solution by creating server.js :
Const jsonServer = require('json-server')
.....
....
....
router.render = function(req, res) {
Var newData = {
TotalCount: res.get('X-Total-Count'),
Result: res.locals.data
}
res.json(newData)
}
from json-server.
There's many way to return a slice of an array, and you're right that's a valid way to do.
There was also a discussion about pagination here: #57
For the moment, I prefer to return an array and meta in headers. But maybe a mock server implementing JSON API would work better with ExtJS (never used this lib)... can't remember right now other mock server names, but there is fortunejs.
Sorry about that.
from json-server.
Is there any solution to do that?
from json-server.
With your solution, you'll not have the "total count", but the number of returned items.
I found a solution using lowdb. It's not perfect because we need to call the db twice (one for the initial request, one for the total count).
For example, I want to count the number of users.
const db = await low(myStorage);
const countItems = resource =>
db
.get(resource)
.map('id')
.value().length;
router.render = (req, res) => {
res.jsonp({
items: res.locals.data.items,
totalItems: countItems('users') // parse url to get the desired resource
});
};
from json-server.
Related Issues (20)
- Issues with installing to non alpha version HOT 3
- How do I set up a unique road handler? HOT 1
- Error on : json-server --watch db.json HOT 1
- query parameter does not working HOT 3
- Weird behaviour when property "period" is in json mock data
- Steps to serve multiple json files with json-server HOT 3
- `_gte` and `_lte` doesn't work for dates HOT 3
- In new version not working `_sort=price&_order=asc` , please fix this issue.
- Multiple filters not working
- Whats cuestión? For the api?
- cors problem HOT 2
- Has the --delay option been removed? HOT 5
- not working at all
- conditional operators doesn't work
- I have a problem opening Json-server --watch.db.json HOT 1
- ?_page= or &_page= no longer working.
- _sort=price&_order=asc no longer working HOT 1
- it doesn't work on xxx.js or remote url,though xxx.json is ok
- SyntaxError: Unexpected token '||=' HOT 1
- Please, bring back the `--delay` option HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-server.