Implement middleware and request validation
Use the
h higher-order function to create handlers with middleware. The first function acts as middleware; if it returns a response (e.g., a 403 error), the request chain stops. Otherwise, it proceeds to the subsequent handler.import { h } from '@buntal/http'
export const GET = h(
(req, res) => {
console.log('Hi, this is a middleware!')
if (req.query.name !== 'John') {
return res.status(403).json({
error: 'Forbidden'
})
}
},
(req, res) => res.json({
pong: 1
})
)