4

I'm trying to use https://www.npmjs.com/package/json-server as a mock backend, I'm able to match URLs for get, but how can i return some mock-response for POST calls.

Like for create user URL will be like

 URL - http://localhost:4000/user 
 Method - POST
 Request Data - {name:"abc", "address":"sample address"}

 expected response - 
 httpStats Code - 200, 
 Response Data - {"message":"user-created", "user-id":"sample-user-id"}

In Some Cases I also want to send custom http codes like 500,423,404,401 etc.. depending upon some data.

Biggest problem is that my code is not returning anything response for POST, its only inserting records in JSON

Anup Singh
  • 1,513
  • 3
  • 16
  • 32
  • 1
    You should not return 5xx errors depending on data. 5xx errors are errors from the web server, they should not depend on data. – Seblor Oct 09 '19 at 07:00

1 Answers1

8

By default POST requests through json-server should give a 201 created response.

If you need custom response handling, you might need a middleware to get hold of req and res object.

Here I'm adding a middleware to intercept POST requests and send a custom response. You could tweak it to your specific case.

// Custom middleware to access POST methods.
// Can be customized for other HTTP method as well.
server.use((req, res, next) => {
  console.log("POST request listener");
  const body = req.body;
  console.log(body);
  if (req.method === "POST") {
    // If the method is a POST echo back the name from request body
    res.json({ message:"User created successfully", name: req.body.name});
  }else{
      //Not a post request. Let db.json handle it
      next();
  }  
});

Complete code (index.js)..

const jsonServer = require("json-server");
const server = jsonServer.create();
const router = jsonServer.router("db.json");
const middlewares = jsonServer.defaults();

server.use(jsonServer.bodyParser);
server.use(middlewares);


// Custom middleware to access POST methids.
// Can be customized for other HTTP method as well.
server.use((req, res, next) => {
  console.log("POST request listener");
  const body = req.body;
  console.log(body);
  if (req.method === "POST") {
    // If the method is a POST echo back the name from request body
    res.json({ message:"User created successfully", name: req.body.name});
  }else{
      //Not a post request. Let db.json handle it
      next();
  }  
});

server.use(router);

server.listen(3000, () => {
  console.log("JSON Server is running");
});

And you can start json-server using node index.js

Nithin Thampi
  • 3,579
  • 1
  • 13
  • 13
  • On the homepage of json-server, http://localhost:3000/ I'm getting "No resources found", its not able to detect any thing from DB.json – Anup Singh Oct 09 '19 at 09:36
  • There should be a file with name "db.json" in the same level as index.js file. If you need a repl... See this.. https://repl.it/repls/PleasedImperfectScientificcomputing – Nithin Thampi Oct 09 '19 at 09:45
  • is there a way to use this with custom routes instead of the db.json? – Kevin Gleeson Jul 03 '20 at 09:19