This repository has been archived on 2025-12-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Andrew Trieu 8af61a9000 Upload 6.20
2023-06-24 16:16:26 +03:00

28 lines
643 B
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()
const validator = (request, response, next) => {
console.log()
const { content } = request.body
if (request.method==='POST' && (!content || content.length<5) ) {
return response.status(400).json({
error: 'too short anecdote, must have length 5 or more'
})
} else {
next()
}
}
server.use(middlewares)
server.use(jsonServer.bodyParser)
server.use(validator)
server.use(router)
server.listen(3001, () => {
console.log('JSON Server is running')
})