New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to wrap responses? #118
Comments
Hi @arqex, thanks for the nice words :) |
Hi, That is really good news. I tweaked json-server locally in order to get it working as I need, but something official is much better :) Thanks |
Just did a quick release ( You can now override the new You can access data before it's returned to the browser using I'll add it to the README later. Feedbacks are welcome :) |
Hi typicode, I looks great, a simple proxy function, not only for wrapping the data, but for customizing the reponse before it is sent. Perfect. Thanks for your awesome work! |
Hi @arqex, You're welcome. I'm happy if it helps. |
Hi, could you please help me out with a simple tutorial on how to wrap the |
Hi @csibar This code should do the job var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
server.use(jsonServer.defaults)
server.use(router)
// If you want to target /posts specifically
router.render = function (req, res) {
if (req.url === '/posts') {
res.jsonp({
posts: res.locals.data
})
} else {
res.jsonp(res.locals.data)
}
}
// Or /resources in general
router.render = function (req, res) {
if (req.method === 'GET' && !req.params.id) {
var obj = {}
obj[req.params.resource] = res.locals.data
res.jsonp(obj)
} else {
res.jsonp(res.locals.data)
}
}
server.listen(3000) |
Hi, thank you very much, now I get it, but it throws an error:
|
I think it's because your js file doesn't export a function. You can use the README example as a starting point: module.exports = function() {
var data = { users: [] }
// Create 1000 users
for (var i = 0; i < 1000; i++) {
data.users.push({ id: i, name: 'user' + i })
}
return data
} If you need help with that or if it doesn't work, you can paste your |
Oh OK, my bad, works like a charm, thank you very much! |
One more thing: |
Oh sorry about that, thanks for letting me know. I've not tested code. |
Unfortunately no, |
@typicode Hi, Great package! Thanks!
|
Hi, thanks for your wonderful json server.
I was wondering if it is possible to modify the server response. For example, if I get
/posts
currently I receive an array of posts. I would like to receive an object with one propertyposts
and the list of post as the value of the property:I have been playing with the middleware but I can't hijack the server response. Any clues?
The text was updated successfully, but these errors were encountered: