Skip to content

Instantly share code, notes, and snippets.

@juanbrujo
Created August 7, 2019 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanbrujo/efdcf6ab9eacbd5b5d69884233d0a3ce to your computer and use it in GitHub Desktop.
Save juanbrujo/efdcf6ab9eacbd5b5d69884233d0a3ce to your computer and use it in GitHub Desktop.
json-server: Custom error (400, 500) when targeting specific route
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('mock/db.json')
const middlewares = jsonServer.defaults()
const port = process.env.PORT || 3000
server.use(middlewares)
// send error message when validating code on invest
server.get('/invest_url', (req, res) => {
console.log('/invest_url, error 400')
res.status(400).jsonp({
error: "Error message"
})
})
server.use(jsonServer.bodyParser)
server.use((req, res, next) => {
if (req.method === 'POST') {
req.body.createdAt = Date.now()
}
next()
})
server.use(router)
server.listen(port, () => {
console.log('JSON Server is running on http://localhost:' + port)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment