Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
¿Cómo puedo agregar un middleware asíncrono a una ruta de enrutador?

Tengo un enrutador express y estoy configurando algunas rutas para él, paso un validador para los datos y un controlador, pero el validador tiene que funcionar con algunas promesas, así que necesito colocar una espera, el código que escribí se ve como esto:

 constructor() { this.router = express.Router(); this.router.use(express.json()); this.router.use(express.text({ type: ['text/plain', 'text/html'] })); } async addCreateTaskRoute(validator, handler) { if (!handler) { throw Error('cannot add empty handler'); } this.router.post('/tasks', await validator, handler); return this; }

¿Te parece bien esta solución? ¿Hay más opciones? El proyecto en el que está incluido es un middleware, y el validador y los controladores provienen de otros módulos y el tipo de ellos es Express RequestHandler

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Su validator debe llamar a next() de forma asíncrona después de una validación exitosa, esto luego invocará el siguiente middleware, que es handler . Después de una validación fallida, se devuelve un error y no se llama a next() para que no se procesen más middlewares.

 async function validator(req, res, next) { var valid = await validationResult(...); if (valid) next(); else res.status(400).end("Error message"); } this.router.post("/tasks", validator, handler);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!