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

163
Views
How to add variables and methods in callback parameters

I'm making a custom middleware package as a project, and I ran into a problem. In one of the files, I have some jsdoc types (trying to make the package small so no typescript) for the callback:

class Response {
  constructor() {
    // Constructor stuff
  }
  /**
   * 
   * @param {string} path 
   */
  async useFile(path, file) {
    const res = await fetch(path, {
      method: 'GET',
      body: fs.readFile(file, (err, data) => {
        if (err) throw err
        else return data
      }),
      headers: {
        'Content-type': 'text/html'
      }
    })
    return JSON.stringify(res)
  }
}

I then import that Response class and use it as a type:

server.get('/', (req, res) => {
  res.useFile('index.html')
})

In my test folder, I'm creating a server and assigning it to this server variable. I then call the get method, using the useFile method on the response:

server.get('/', (req, res) => {
  res.useFile('index.html')
})

But when I run the code get this error:

res.useFile('/', './index.html')
      ^

TypeError: Cannot read properties of undefined (reading 'useFile')

I know that the res param is undefined (because it's a parameter), so the useFile method won't work, but I've seen other middleware packages like express do this without problems. How do I put these methods onto the params?

about 4 years ago · Juan Pablo Isaza
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!