I am trying to read a file content based on a GET request with the file name as an input parameter: My code should support nested folders also. If the file is not present then I want to return 404, for any other error I want to return 500.
This is my code:
const loadData = (folderPath) => {
//here my code:
fs.readFileSync(path.join(_dirname, folderPath));
}
I get an error saying _dirname is not defined.
This is my test code:
const get = (file) => new Promise((resolve, reject) => {
return http.get(`http://localost:3000/${file}`, res => {
let data = "";
let statuc = res.statusCode;
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
resolve({data, statuc});
});
});
});
const server = loadData('tmp/myapp/public');
const port = process.env.PORT || 3000;
server.listen(port);
get('myfile.txt').then(data => {
console.log(data);
server.close();
});