You could use the native fs.readFile()
like this:
var fs = require("fs");
app.get('/aa', function(req, res) {
var filePath = 'C://test.txt';
fs.readFile(filePath, 'utf8', function(err, data) {
if (err) {
res.send(err);
} else {
console.log(data);
res.send(data);
}
});
});
So, this is pretty much the same amount of code as what you had, the main difference being that this version has full error handling whereas your version did not handle errors from fs.readFile()
and the browser request was left permanently with no response upon error.
Creating modules to contain shared code is a generally good idea. But, when working with async functions and their responses, you need to make sure you preserve full async response and error handling when you write cover functions that use async library functions. In this case, you missed some of that, rendering your cover function not fully functional.
In addition, when covering a built-in function, you should pretty much make sure you're adding enough value to justify a new interface. Simple defaulting one argument is probably not enough change/improvement to justify creating a whole new set of functions that someone learning or modifying your code would have to become familiar with in order to be productive in your code (which would not be an issue if you just used the standard functions).