0

currently i'm trying to do some post req into my server (made with express) and my goal is when the user input something in the field (like a simple search) i receive this in the req.body and check it inside the file to see the match like the example below:

CFH303 MUS7404 - CFH304 MUS7404 - CFH305 CSO7918 - CFH306 GCN7127 - CFH307 MEN7108 - CFH308 MEN7022 - CFH309 PSI7403 - CFH310 GCN7123

Each of these bold text is the result that i'm looking for, the input will be the itallic text and i must return the bold one. My file does not have more than one string in the same line, they are line by line (the hypens i made here in stack to better view).

I found other question but it's on fs.createReadStream is it a better approach? Because in my mind makes zero sense receive them one by one.

My route, here i was trying to do some kind of array but i notice the \n pushing togheter

router.post('/getIdDisciplina', function(req, res, next){
  let idDisc = req.body.IdDisc;
  let fileSize = fs.statSync('arqex.txt').size;

  console.log(idDisc);
  fs.open('arqex.txt', 'r', function(err, fd){
    if (err) throw err;
    else{
      fs.readFile('arqex.txt', 'utf8', function(err, data){
        if (err) throw err;
        else{
          //let aux = []
          //aux.push(data)
          //console.log(aux);
        }
      });
      fs.close(fd, (err) =>{
        if (err) throw err;
      });
    }
  });
  res.render('index');
});

Thanks for the time!

  • read the file and store the data of file in json (use global variable outside router). Inside router check the key and get the result. fs.readFileSync(path[, options]) is what I usually use – Edwin Babu Aug 18 '18 at 04:36
  • Sorry for the delay, some busy days and a little of struggle solving the problem, but in the end i managed to do it, i don't know if you'd like to post as an answer and elaborate a little but i'd accept as the right one. Thank you Edwin! – Gabriel Domene Aug 22 '18 at 11:20

0 Answers0