Closed rluvaton closed 2 years ago
Why would you not execute the validations?
I want to execute the validators I just have logger that log every request and if request fails on the validation than it won't log
I don't understand the question.
In case I have the code below - use express and validator:
and I send this request (name
is missing in body):
curl -v -d '{}' -H 'Content-Type:\ application/json' http://localhost:5555/
The logger won't log, but if I change to preValidation
the log will happen, so why you suggested to use preHandler
and not preValidation
is there something I'm missing here
const Fastify = require('fastify');
const expressPlugin = require('@fastify/express');
const fastifyFormBody = require('@fastify/formbody');
(async () => {
const fastify = Fastify({});
await fastify.register(fastifyFormBody);
await fastify.register(expressPlugin, {
expressHook: 'preHandler',
});
// This is actually express-winston middleware but just for the sake of example
fastify.use((req, res, next) => {
const resEnd = res.end;
res.end = function (...args) {
console.log(`[${res.statusCode}] ${req.method} ${req.url}`, req.body);
return resEnd.apply(res, args);
};
next();
});
fastify.post(
'/',
{
schema: {
body: {
type: 'object',
properties: {
name: {
type: 'string',
},
},
required: ['name'],
},
},
},
async (request, reply) => {
reply.send({});
}
);
const address = await fastify.listen({
port: 5555,
});
console.log(`server listening on ${address}`);
})()
.then(console.log)
.catch(console.error);
Thanks
What you say makes total sense, for your use case preValidation
is better.
Prerequisites
Issue
In the docs there is this example in the Troubleshooting - POST request with body hangs up:
Why the
expressHook
should bepreHandler
and notpreValidation
?if it's
preHandler
, middlewares that want to always run won't (for example - logger [we are planning to migrate topino
:)])and
preValidation
is the first hook that have the body parsed, no?