Json-server: Json post response body

Created on 25 May 2016  路  8Comments  路  Source: typicode/json-server

Hi,

I'd like to ask if it is possible for json server to return a response body when doing post?
I want to do a search using an id, Sample below:
My Request body:
{id: '1*'}

I want my post to return:
/tasks: [{
'id': 1,
'name':john},
{ 'id': 11,
'name': jess
}
]

Is this possible for json-server?

Thanks

Most helpful comment

Sure, here's an example how to do it. The idea is to add a new route and query the database based on the POST payload.

var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()

server.use(middlewares)

server.post('/tasks', function (req, res) {
  var db = router.db // lowdb instance
  var tasks = db
    .get('tasks')
    .filter(function (task) {
      var re = new RegExp(req.body.id)
      return re.test(task)
    })
    .value()

  res.jsonp(tasks)
})

server.use(router)
server.listen(3000, function () {
  console.log('JSON Server is running')
})

Let me know if it works for you.

Note: currently json-server is using lowdb v0.13.0-beta.4, so the API is not the same as the one you can see on lowdb master README.

All 8 comments

Hi @fuentesab,

Not with POST, but you can use GET and _like operator

/task?id_like=1 // will return tasks with id 1, 10, 11, ...

http://jsonplaceholder.typicode.com/posts?id_like=1

If you really need to have it on POST, you can customize JSON Server by using it as a module.

Hi,

Appreciate the reply.
Can you pls elaborate a little onhow to use it as a module?

Cause i really need to use POST on this one.

Thanks a lot

Sure, here's an example how to do it. The idea is to add a new route and query the database based on the POST payload.

var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()

server.use(middlewares)

server.post('/tasks', function (req, res) {
  var db = router.db // lowdb instance
  var tasks = db
    .get('tasks')
    .filter(function (task) {
      var re = new RegExp(req.body.id)
      return re.test(task)
    })
    .value()

  res.jsonp(tasks)
})

server.use(router)
server.listen(3000, function () {
  console.log('JSON Server is running')
})

Let me know if it works for you.

Note: currently json-server is using lowdb v0.13.0-beta.4, so the API is not the same as the one you can see on lowdb master README.

I was looking for basically this same answer... it worked for me, thanks @typicode !!

I am using this also. Thank you for the example. I did find that you need to call server.use(router) after your statements for catching the post/get/etc.

Thank you @typicode it's working fantastic :)

Sorry to hijack your post, but I'm having some issues too. When I follow @typicode's example, req.body` is undefined. Here's my code:

server.post('/billing', (req, res) => {
  console.info(req.body);
  // Logs undefined
  ...
});

and here's my request payload:

{
  "planId":"std_monthly",
  "quantity":705
}

I'm using version 0.9.5. Any help would be greatly appreciated! I'd also be happy to post the log of just req if that would help :)

In case anyone else ends up here, I had to follow what was in this post to get req.body to give anything

https://stackoverflow.com/questions/9177049/express-js-req-body-undefined

Main parts are these
const bodyParser = require("body-parser"); // didn't need to install because express is part of this solution

// create application/json parser
const jsonParser = bodyParser.json();

// create application/x-www-form-urlencoded parser
const urlencodedParser = bodyParser.urlencoded({ extended: false });

// you use which one works for your situation

// my post that is BEFORE server.use(router);
server.post("/api/auth", urlencodedParser, function (req, res) {
console.info(req.body);
res.send("hi");
});

Was this page helpful?
0 / 5 - 0 ratings