I have the following application:
// para usar cliente y servidor http
const http = require("http");
// Para trabajar las url
const url = require("url");
// para leer archivos
const rf = require("./read_files");
const hostname = "127.0.0.1";
const port = 3000;
const server = http.createServer((req, res) => {
statusCode = 200;
contentType = {'Content-Type': 'text/html'};
let t = rf.read_file("text.html");
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(t);
res.end();
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
And I have the following module called read_files:
const fs = require("fs");
module.exports = {
read_file: function(path){
let fs_data = fs.readFile(path, null, (
(err, data),=>{
if(err){
throw err;
}
})
);
return fs_data;
}
}
What I want is to be able to return the data value that is in the fs callback, but when I check outside of that call it shows up as undefined.
How can I get back from the read_file function of the read_files module to the main application?