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.
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With