Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
How to pass req.params as an an argument to a middleware function?

I'm trying to figure out a way to use req.params as an argument in my middleware. Take this (obviously broken) code for example:

router.post('/:myParam', checkSchema(schemas[req.params.myParam]), async (req, res, next) => {
  // do stuff
})

The goal here is that I am using express-validator and I load a dynamic schema based on what param is passed. The above code is obviously broken because I don't yet have the scope to access the req variable, I'm just trying to illustrate what I'm trying to accomplish.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

if you know the possible params ahead, you could do something like the following:

router.post("/:myParam", checkSchema("soccer"), async (req, res, next) => {});

//checkSchema.JS
    const soccerSchema = require("../schemas/soccerSchema");
    const swimmingSchema = require("../schemas/swimmingSchema");

    module.exports = function (schemaName) {
      return (req, res, next) => {
        const schemas = { soccer: soccerSchema, swimming: swimmingSchema };
        //You can access it here schemas[schemaName]
        console.log(schemas[schemaName]);
        next();
      };
    };

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can directly call schemas(req.params.myParam) inside the checkSchema middleware since the middleware will have access to the request object.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda