在 Express 中更新文件
我是 NodeJs 新手,并且创建了一个 Node 应用程序。用户必须从 ui 上传 html 文件..然后我应该根据一些要求更新该文件以使页面可以工作。
考虑一个示例:-
<meta name="description" content="">
<title>xyz</title>(The xyz can be any word)
上传的文件包含这两行,我想删除第一行并将另一行替换为我需要的行。
我尝试过,
var fs = require('fs')
fs.readFile("./index.html", {encoding: 'utf8'}, function (err,data) {
var formatted = data.replace("<title>xyz</title>", 'This new line replaces the old line');
fs.writeFile("your file", formatted, 'utf8', function (err) {
if (err) return console.log(err);
});
});
我不知道该怎么做..但是随着 xyz 的值发生变化,上述函数不起作用..因此我怎样才能使其泛化。
感谢您的帮助。
I am new to NodeJs and have created a Node application. The user has to upload the html files from the ui.. and then I am supposed to update the file with a few requirements to make the page workable.
Considering an Example :-
<meta name="description" content="">
<title>xyz</title>(The xyz can be any word)
The Uploaded File contains these two lines, And I want to remove the first line and replace the other line with the lines that i require to be there.
I tried
var fs = require('fs')
fs.readFile("./index.html", {encoding: 'utf8'}, function (err,data) {
var formatted = data.replace("<title>xyz</title>", 'This new line replaces the old line');
fs.writeFile("your file", formatted, 'utf8', function (err) {
if (err) return console.log(err);
});
});
I don't Know How to do it .. But as the value of xyz changes the above function does not work.. Therefore how can I make it generalized.
Thanks For Any Help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您好,我有两个解决方案:
我将回答第二个,因为我认为在这种情况下更好:
Hi I have two solutions in mind:
i'm gonna answer with the second one because i think it's better in this case: