facg3 / msn-AutoComplete

0 stars 0 forks source link

why reading the file before getting the data from the front end #23

Open Walidmash opened 6 years ago

Walidmash commented 6 years ago

https://github.com/facg3/msn-AutoComplete/blob/8c372d2136eec1a1ef9a7ffdb6da271cd51ef2f3/src/handler.js#L41

in this function you are reading the file first and then you start streaming the data and checking it, you should get the data first and if there is no error in the data or the connection start opening the file, alway try to reduce your request on the files or database. the code should look like this

let allValues = "";
      request.on('data', chunckOfData =>{
        allValues += chunckOfData;
      });
      request.on('error', ()=>{
         response.writeHead(500,'Content-Type : text/html');
         response.end('<h1> Internal server Error </h1>');
      });
      request.on('end', ()=>{
        fs.readFile(path.join(__dirname + '/countries.json'), (error,result) =>
        {
       if(error){
         response.writeHead(500,'Content-Type : text/html');
        response.end('<h1> Internal server Error </h1>');
       }
       else {
        ..................handle the filter
       }
     });
   });
Salam-Dalloul commented 6 years ago

done

Walidmash commented 6 years ago

it still the same https://github.com/facg3/msn-AutoComplete/blob/6bf8378a550c4870512a5bbe1ea4bf1bde418f83/src/handler.js#L40