Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.