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?