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

113
Visualizações
Abstract the same method parameters which are called everytime

I have one method that helps responding to requests. Its signature is as follows:

function render(req, res, data, kind, route)

It is being called dozens of times with different parameters. I would like to have a cleaner code where I would call

render(data, kind, route) knowing that req and res are always there in the context when calling.

As the following

router.get('/', async function (req, res, next) {
   // some logic
   // render({}, 'kind', 'route') instead of
   render(req, res, {}, 'kind', 'route')
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Following @deceze advice. I ended up using a middleware that is attaching a closure as the following

const makeRenderer = (req, res, next) => {
  res.locals.renderer = (data, kind, route) => renderer(req, res, data, kind, route)
  next()
}


router.get('/', makeRenderer, async function (req, res, next) {
  const listings = await mongoQueries.getDocumentsSince(
    20, '', req.body.pagination)
  const { page, perPage } = req.body.pagination
  const data = {
    listings: listings.documents,
    addressPoints: [],
    current: page,
    pages: Math.ceil(listings.count / perPage)
  }
  res.locals.renderer(data, 'listings', 'listings')
})
about 4 years ago · Santiago Gelvez 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