Node.js如何编写高效文件处理模块的代码?

2026-04-06 11:181阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计205个文字,预计阅读时间需要1分钟。

Node.js如何编写高效文件处理模块的代码?

javascriptindex.jsconst fs=require('fs');const common=require('./library/common');const fileHandle=require('./library/fileHandle');/** * 读取处理文件夹中的文件 */let path='/Users/mingwang/tmp/docs/nodejs.org/api';fs.readdir(path, ...

Node.js如何编写高效文件处理模块的代码?

index.js

const fs = require('fs'); const common = require('./library/common'); const fileHandle = require('./library/fileHandle'); /** * 读取处理文件夹里的文件 */ let path = '/Users/mingwang/tmp/docs/nodejs.org/api'; fs.readdir(path, (err, files) => { files.forEach((v) => { // 只处理文件 let filePath = path + '/' + v; if (common.isFile(filePath)) { fileHandle.removeGoogleAnalyze(filePath); } }); }); fileHandle.js

const readline = require('readline'); const fs = require('fs'); module.exports = { /** * 移除 谷歌统计 相关的脚本 * @param file */ removeGoogleAnalyze: (file) => { // 按行读取文件 const rl = readline.createInterface({ input: fs.createReadStream(file) }) // 保存内容 let html = ''; rl.on('line', (line) => { if (line.indexOf('dnt_helper') < 0) { html = html + line + '\n'; } }); // 读取完成的时候,写入文件 rl.on('close', () => { fs.writeFile(file, html); }); } } common.js - 见公共模块

common.js - 见公共模块

本文共计205个文字,预计阅读时间需要1分钟。

Node.js如何编写高效文件处理模块的代码?

javascriptindex.jsconst fs=require('fs');const common=require('./library/common');const fileHandle=require('./library/fileHandle');/** * 读取处理文件夹中的文件 */let path='/Users/mingwang/tmp/docs/nodejs.org/api';fs.readdir(path, ...

Node.js如何编写高效文件处理模块的代码?

index.js

const fs = require('fs'); const common = require('./library/common'); const fileHandle = require('./library/fileHandle'); /** * 读取处理文件夹里的文件 */ let path = '/Users/mingwang/tmp/docs/nodejs.org/api'; fs.readdir(path, (err, files) => { files.forEach((v) => { // 只处理文件 let filePath = path + '/' + v; if (common.isFile(filePath)) { fileHandle.removeGoogleAnalyze(filePath); } }); }); fileHandle.js

const readline = require('readline'); const fs = require('fs'); module.exports = { /** * 移除 谷歌统计 相关的脚本 * @param file */ removeGoogleAnalyze: (file) => { // 按行读取文件 const rl = readline.createInterface({ input: fs.createReadStream(file) }) // 保存内容 let html = ''; rl.on('line', (line) => { if (line.indexOf('dnt_helper') < 0) { html = html + line + '\n'; } }); // 读取完成的时候,写入文件 rl.on('close', () => { fs.writeFile(file, html); }); } } common.js - 见公共模块

common.js - 见公共模块