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

118
Views
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 answers
Answer question

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 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!