json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Can the POST method return a custom JSON result instead of returning the requested JSON?

Open lasyka opened this issue 1 year ago • 1 comments

Hi there! Can you make the POST method to return a custom JSON result data in db.json not just return the requested JSON data? Thx!

lasyka avatar Nov 22 '24 05:11 lasyka

To do this, you need to use middleware, where you will map the POST route so that all transmissions to that URI are returned in the format you want.

`module.exports = function (req, res, next) { const endpointRoutesURI = { 'api/entry-route': require('./my-router-with-return-new-data-post') }

const endpoint = Object.keys(endpointRoutesURI).find((key) =>
  req.originalUrl.startsWith(key)
);

// Here you work with the rest of the logic. }

gleissonneves avatar Oct 24 '25 17:10 gleissonneves