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

Add custom router for custom respond

Open phineasla opened this issue 3 years ago • 0 comments

I find that overriding router.render is cumbersome, so I changed it to using express router, this will keep the lowdb operations while allowing custom responses. I don't know if anyone has tried this before.

Usage:

server = jsonServer.create()
customRouter = express.Router()
router = jsonServer.router(db, { customRouter })
server.use(jsonServer.defaults())
server.use(router)

customRouter.post('/posts', (req, res) => {
  res.jsonp({ customData: 'hello' })
})

Related issues:

  • typicode/json-server/issues/717
  • typicode/json-server/issues/1002

phineasla avatar Jun 17 '22 03:06 phineasla