I'm trying to reuse/call function but with one additional parameter in the middle
const fs = require('fs');
module.exports = (filePath, res) => {
fs.readFile(filePath, (err, data) => {
if (err) {
console.log(err);
res.writeHead(404, {
'Content-Type': 'text/plain'
});
res.write('Page not found!');
res.end();
return;
}
res.writeHead(200, {
'Content-Type': 'text/html'
})
res.write(data);
res.end();
})
}
In next case I have to add additional parameter when reading file.
fs.readFile(filePath, utf-8, (callback) => {})...
Is it possible to make new function with bind or call it with an additional argument in my case is in the middle I need to add encoding utf-8 without modifying the function because is wrapped as module. Or perhaps I need to change the function to work with two or three parameters. In short my goal is using the same function with minor changes because it's 90 % the same.
EDIT:
I need to use the same function for reading file but this time needs to add encoding. So instead of fs.readFile(filePath, (callback) => {})
should be fs.readFile(filePath, 'UTF-8', (callback) => {})
You can see what I'm trying to do in github. This is the file for serving static files
This is the function I'm trying to reuse
https://github.com/xakepa/SoftUni/blob/Beta/JS%20WEB/Node%20JS/Cat%20Shelter/handlers/readHtml.js