5

I use fs writefile method to write file to node server. It works on my computer without a problem. But it doesn't work on production (heroku).

The code below work on my local network but doesn't work on heroku. When I run it on heroku there is no error, but it doesn't overwrite the exiting file.

router.post("/writefile/overwritefile",function(req,res){     
    if(req.body.bult1){
             var bult1red = req.body.bult1;
             var removenewline = bult1red.replace(/(\r\n|\n|\r)/gm,"");  // remove new line space 
         var convertoarray = removenewline.split(',');  // convert the string to an array 
         var i = 0;
         var buls = "";
         convertoarray.forEach(function(element, index, array){
              buls+= "<li>" +  element + "</li> \n";
               i++;
              if (i === array.length){
                     bult1 = '<ul> \n'+
                                 buls + 
                              '</ul>\n';                                                    
               }
           });
           }else{
                  bult1 = "";
            }

     var writethis = '<!DOCTYPE html> \n'+
            '<html>\n'+
            '<head>\n'+
              '<meta charset="UTF-8">\n'+
          '<meta name="viewport" content="width=device-width, initial-scale=1.0">\n'+
          '<meta http-equiv="X-UA-Compatible" content="ie=edge">\n'+
          '<title> testing fs write </title> \n'+             
          '<style>\n'+
            'content{\n'+
                'border: 20px solid #bdc3c7;\n'+ 
                'padding: 20px;\n'+
                'width: 80%;\n'+
                'margin: 20px auto;\n'+ 
            '}\n'+
         '</style>\n'+
         '</head>\n'+             
            '<body>\n'+
                '<div class="content">\n'+
                        bult1 +  '\n'+
                '<div/> \n' +       
           '</body>\n'+
       '</html>\n';
     
      fs.writeFile('views/files/testwritebeg.ejs', writethis, (err) => {  
                      if (err) throw err;          
            });   
            res.redirect("/");
 });

Is there any limitation using fs writefile system on heroku server? I couldn't know the reason why the above code doesn't work on heroku. Any help please.

Fnr
  • 2,096
  • 7
  • 41
  • 76
wzwd
  • 169
  • 2
  • 10
  • When I have this problem the first thing I check (because it's usually the problem) is that the location I am trying to save it to actually exists on the server. As in is `views/files/` an actual directory – Katie.Sun Nov 28 '18 at 13:35
  • 1
    filepath should begin with `__dirname` to properly point to a directory – Vijay Singh Nov 28 '18 at 13:37
  • yes definitely the directory exist that i used to write file. It works on my local network. The problem is on heroku server. – wzwd Nov 28 '18 at 13:41
  • I had kind of the same issue and I came across [this answer](https://stackoverflow.com/a/44547601/10864272). From the comments under it, I found out that if the folder is empty, it never gets to `Github` and never gets to `Heroku`. A recommended fix would be to put an arbitrary file in there like a `.gitignore`, referenced from this [answer](https://stackoverflow.com/a/8282069/10864272). – Dev Yego Nov 11 '20 at 18:10
  • Does this answer your question? [Appending files in node.js on heroku](https://stackoverflow.com/questions/19839293/appending-files-in-node-js-on-heroku) – Mark Rotteveel Feb 11 '21 at 17:16

1 Answers1

3

when you using fs you should avoid relative path and use absolute path. you can use __dirname and __filename. so you can test this:

  fs.writeFile(`${__dirname}/views/files/testwritebeg.ejs`, bult1 , (err) => {                  
                if (err) throw err;                           
               });    
amir
  • 2,443
  • 3
  • 22
  • 49
  • @Vijay Singh the problem is not using the directory. I use absolute directory using `path.resolve` but it dose not work. In my question the first code work but the second code doesn't work. – wzwd Nov 28 '18 at 15:23
  • @wzwd are you sure that `path` already exist? maybe you should firt creat path. – amir Nov 29 '18 at 09:59
  • @amir yes i create a path using `path.resolve` and use it in the above code then i try it but it doesn't work. – wzwd Nov 29 '18 at 12:06