在 Express 中更新文件

发布于 2025-01-11 10:59:56 字数 689 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2025-01-18 10:59:56

您好,我有两个解决方案:

  1. 使用正则表达式
  2. 使用 html 解析器,例如 https:// /www.npmjs.com/package/node-html-parser

我将回答第二个,因为我认为在这种情况下更好:

const http = require('http');
const fs = require('fs')
const parser = require( 'node-html-parser');
const {parse} = parser

const server = http.createServer((req, res) => {
    fs.readFile("./index.html", {encoding: 'utf8'}, function (err,data) {
    var root = parse(data) 
    // don't forget to use 'myTitle' as id in your html file
    root.querySelector("#myTitle").replaceWith("what you wanna replace with")
    fs.writeFile("your file", root.toString(), 'utf8', function (err) {
    if (err) return console.log(err);
 });
});
    res.end('file modified!');
});

server.listen( 3000);

Hi I have two solutions in mind:

  1. use a regular expression
  2. use an html parser like https://www.npmjs.com/package/node-html-parser

i'm gonna answer with the second one because i think it's better in this case:

const http = require('http');
const fs = require('fs')
const parser = require( 'node-html-parser');
const {parse} = parser

const server = http.createServer((req, res) => {
    fs.readFile("./index.html", {encoding: 'utf8'}, function (err,data) {
    var root = parse(data) 
    // don't forget to use 'myTitle' as id in your html file
    root.querySelector("#myTitle").replaceWith("what you wanna replace with")
    fs.writeFile("your file", root.toString(), 'utf8', function (err) {
    if (err) return console.log(err);
 });
});
    res.end('file modified!');
});

server.listen( 3000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文