[英]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.