Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
Array of functions with additional arguments in the express router middleware
const cb0 = function (data, req, res, next) {
  console.log('CB0')
  next()
}

const cb1 = function (data, req, res, next) {
  console.log('CB1')
  next()
}

app.get('/example/d', [cb0, cb1], (req, res, next) => {
  console.log('the response will be sent by the next function ...')
  next()
}, (req, res) => {
  res.send('Hello from D!')
})

From the above code, I'm passing an array of functions [cb0, cb1] and each function requires some data attribute of type any and additional parameters like req, res and next.

Earlier,I tried passing the data attribute like below format using bind concept. app.get('/example/d', [cb0.bind(data), cb1.bind(data)], (req, res, next)

But if I use the bind concept, then how to pass the other required attributes (req, res and next)? Is there any other way to pass all the parameters, including data without bind? or do we have any limitations in using the array of functions in the express?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First, you've used bind incorrectly (for how your functions are written): The first argument to bind is the value to use as this when calling the function; it's only the subsequent arguments that define arguments to give the function when called. So you'd want cb0.bind(null, data) rather than cb0.bind(data).

But if I use the bind concept, then how to pass the other required attributes (req, res and next)?

(They're arguments, not attributes.) Express does that when it calls your function. The arguments will follow the arguments you've "baked into" your functions via bind. Your functions are already set up correctly to handle that order (data, req, res, next), so with the change, you should be good to go.

So:

app.get('/example/d', [cb0.bind(null, data), cb1.bind(null, data)], (req, res, next) => {
    // ...

Just for clarity, here's an example of a function with data bound to it via bind being called with further arguments:

function example(data, other) {
    console.log(`data = ${JSON.stringify(data)}, other = ${JSON.stringify(other)}`);
}

const ex = example.bind(null, "baked in arg");

// Emulating Express calling the middleware:
ex("passed arg");


Side note: You don't have to put your middleware functions in an array, express is happy with them as discrete arguments instead:

app.get('/example/d', cb0.bind(null, data), cb1.bind(null, data), (req, res, next) => {
    // ...

Either way is fine.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda