Суть в том, что код ниже
const fs = require('fs')
const ReadFile = () => {
let data = fs.readFileSync('a.txt');
console.log('U liked '+data+' times');
}
const GetCon = (app) => {
app.post('/Likes', function(req, res) {
ReadFile();
})
}
module.exports = GetCon;
выводит ошибку:
Error: ENOENT: no such file or directory, open 'a.txt'
at Object.openSync (fs.js:439:3)
at Object.readFileSync (fs.js:344:35)
at ReadFile (/home/sepezho/Documents/Portfolio/src/Likes/Response.js:4:19)
at /home/sepezho/Documents/Portfolio/src/Likes/Response.js:10:9
at Layer.handle [as handle_request] (/home/sepezho/Documents/Portfolio/node_modules/express/lib/router/layer.j s:95:5)
at next (/home/sepezho/Documents/Portfolio/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/sepezho/Documents/Portfolio/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/sepezho/Documents/Portfolio/node_modules/express/lib/router/layer.js:95:5)
at /home/sepezho/Documents/Portfolio/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/sepezho/Documents/Portfolio/node_modules/express/lib/router/index.js:335:12)
А обычное чтение файла, как ниже:
const fs = require('fs')
let data = fs.readFileSync('a.txt');
console.log('U liked '+data+' times');
Работает как надо. Я не могу понять в чем дело. В интернете решение такой проблемы еще не находил.