简体   繁体   English

为什么 fs 写文件在 localhost 中工作但在 heroku 应用程序中不起作用?

[英]Why fs write file working in localhost but not working in heroku app?

I use fs writefile method to write file to node server.我使用 fs writefile 方法将文件写入节点服务器。 It works on my computer without a problem.它在我的电脑上运行没有问题。 But it doesn't work on production (heroku).但它不适用于生产(heroku)。

The code below work on my local network but doesn't work on heroku.下面的代码适用于我的本地网络,但不适用于 heroku。 When I run it on heroku there is no error, but it doesn't overwrite the exiting file.当我在 heroku 上运行它时没有错误,但它不会覆盖现有文件。

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?在heroku 服务器上使用fs writefile 系统有什么限制吗? I couldn't know the reason why the above code doesn't work on heroku.我不知道为什么上面的代码在 heroku 上不起作用。 Any help please.请任何帮助。

when you using fs you should avoid relative path and use absolute path.使用fs ,应避免使用相对路径并使用absolute路径。 you can use __dirname and __filename .您可以使用__dirname__filename so you can test this:所以你可以测试这个:

  fs.writeFile(`${__dirname}/views/files/testwritebeg.ejs`, bult1 , (err) => {                  
                if (err) throw err;                           
               });    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点应用程序可以在localhost上运行,但不能在Heroku上运行吗? - Node app working on localhost but not on Heroku? 图像上传功能在部署的 Heroku 应用程序上不起作用,但在 Localhost 上起作用? - Image upload functionality not working on deployed Heroku app but working on Localhost? 从“fs”读取和写入文件不能异步工作 - Read and Write files from 'fs' not working asynchronously SocketIO在Heroku环境中不起作用[在Localhost中工作] - SocketIO is not working in Heroku Environment [Works in Localhost] CSS和JS正在使用localhost,但不在Heroku Webserver上 - CSS and JS are working on localhost, but not on Heroku Webserver 为什么 fs.copyFile 在 Nodejs 终端中不起作用? - Why fs.copyFile is not working in Nodejs terminal? 节点fs.readdir无法在Grunt文件中工作 - Node fs.readdir not working in Grunt file nodejs fs createWriteStream不适用于文件路径前缀 - nodejs fs createWriteStream not working with file path prefix 反应 js localhost 工作,但 heroku 不适用于客户电子邮件发送 - react js localhost working but heroku not working for customer email sending Socket.io 在本地主机上工作但在部署 heroku 后不工作? - Socket.io is working on localhost but not working after deploying heroku?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM