当前位置: 首页 > news >正文

网站开发流程包括网站在线优化工具

网站开发流程包括,网站在线优化工具,wordpress order插件,网站的版权信息个人博客 Nodejs下动态加载文件夹下的文件模块 个人博客,求关注,如果有不够清晰的,麻烦指出来,如果有很不正确的,多谢批评。 文章概叙 本文应用的场景是读取指定文件夹下面的所有文件,然后做操作&#…

个人博客

Nodejs下动态加载文件夹下的文件模块

个人博客,求关注,如果有不够清晰的,麻烦指出来,如果有很不正确的,多谢批评。

文章概叙

本文应用的场景是读取指定文件夹下面的所有文件,然后做操作,用到的方法是fs模块的readdir,以及require作为加载文件的例子

应用场景

讲一个大概的例子,比如现在一个系统,作用是对输入的数据做处理,根据不同的来源,做不同的处理,但是现在的逻辑只有两种处理方式,我们可以直接用一个简单的if-else或者Map做处理。但考虑到项目的扩展性,我们决定使用模块化的处理方式,即将不同的处理分装为一个js文件,接着匹配不同的行为准则,而这样做的好处就是其他人不需要再去改入口文件的代码,只需要插入新的模块就可以了,其实这也就是很标准的一种模块化思想。

数据准备

首先,先看下文件的架构,index.js是入口文件,而a.js以及b.js则是模块文件。a.js以及b.js都只是打印出一句话而已。
在这里插入图片描述

在这里插入图片描述

代码实现

实现过程不复杂,大概地讲解下,做一个简单的demo,大家就会明白如何处理了​。
第一步,先用fs.readdir获取到指定文件夹下的所有文件名字。

const fs = require("fs");const currentDirPath=__dirname;
​
fs.readdir(currentDirPath, (err, files) => {if (!err) {console.log(files);}
})

readdir的参数主要有两个,一个是文件夹的路径,这儿我用当前的文件夹的路径,也就是代码中的currentDirPath。
第二个参数是回调参数,也就是我们的callback function,其中callback的第一个参数是boolean类型的err,也就是否报错,第二个参数则是文件名称的列表。
当前的打印如下
在这里插入图片描述

第二步,我们需要排除现在所属的index.js文件,当然不会那么low bee到去写一个值为’index.js’的常量,我们可以用__filename来获取当前文件的路径。
首先,看下打印出来__filename的值是什么
在这里插入图片描述

因此,我们还需要将前面的文件的路径以及最后的""去掉,只留下最后的文件名字,让下面的文件列表做循环的时候,能完美的匹配到我们当前文件的名字,最后的代码如下。

const fs = require("fs");const currentDirPath = __dirname;
const currentFilePath = __filename
// 获取当前文件的名字,包含扩展名。
const currentFileName = currentFilePath.replace(currentDirPath, '').substring(1);// 获取指定文件夹下面的所有文件
fs.readdir(currentDirPath, (err, files) => {if (!err) {// 遍历文件for (let index = 0; index < files.length; index++) {const element = files[index];// 如果当前入口文件if (element !== currentFileName) {console.log(element);}}}
})

打印效果如下

在这里插入图片描述

第三步是最重要的加载模块,使用ts的朋友都知道我们加载文件的时候用的是import。而不是require。但是很遗憾,我这儿还是需要用require来加载(新博客会讲解下如何按需、动态加载)。原因也很简单,使用require是最简单的,这样子别人很容易就能接受,但是我也会大概的写下如何用import加载。

const fs = require("fs");const currentDirPath = __dirname;
const currentFilePath = __filename
// 获取当前文件的名字,包含扩展名。
const currentFileName = currentFilePath.replace(currentDirPath, '').substring(1);// 获取指定文件夹下面的所有文件
fs.readdir(currentDirPath, async (err, files) => {if (!err) {// 遍历文件for (let index = 0; index < files.length; index++) {const element = files[index];// 如果当前入口文件if (element !== currentFileName) {console.log(element);// require 加载require(`./${element}`);// import 动态加载, 需要注意使用await的作用。//  await import(`./${element}`);}}}
})

大体就是这样,自己敲一遍,你会发现没什么,因为真的没什么难度。只要熟悉各类的api都没啥问题,顶多是吃在了不熟悉api的亏上。但也并非百试百灵,比如在用mocha做单元测试的时候,我就不建议使用这种方式。因为超纲,我就不写怎么在mocha里面使用了

注意点

应该有朋友注意到,我专门使用了for…而不是使用最简单的forEach。结合到后面的await,我想大家也能猜到是因为async/await在for中才能起作用的原因。​​


文章转载自:
http://dinncounexorcised.bpmz.cn
http://dinncomassoretic.bpmz.cn
http://dinncosocket.bpmz.cn
http://dinncomangy.bpmz.cn
http://dinncowhitehanded.bpmz.cn
http://dinncoflaw.bpmz.cn
http://dinncobanka.bpmz.cn
http://dinncopaul.bpmz.cn
http://dinncoeconomics.bpmz.cn
http://dinncorainwater.bpmz.cn
http://dinncocarbonado.bpmz.cn
http://dinncothivel.bpmz.cn
http://dinncoshatter.bpmz.cn
http://dinncofarmworker.bpmz.cn
http://dinncohandbookinger.bpmz.cn
http://dinncomayonnaise.bpmz.cn
http://dinncosaxophonist.bpmz.cn
http://dinncostipulate.bpmz.cn
http://dinncowentletrap.bpmz.cn
http://dinncoshutout.bpmz.cn
http://dinncopropagandism.bpmz.cn
http://dinncosedulous.bpmz.cn
http://dinncocarabin.bpmz.cn
http://dinncocapitalize.bpmz.cn
http://dinncoindiscriminating.bpmz.cn
http://dinncohousebreak.bpmz.cn
http://dinncoapolune.bpmz.cn
http://dinncoexclusionist.bpmz.cn
http://dinncomachine.bpmz.cn
http://dinncoleaves.bpmz.cn
http://dinncominischool.bpmz.cn
http://dinncoqueenlet.bpmz.cn
http://dinncopedocal.bpmz.cn
http://dinncolevoglucose.bpmz.cn
http://dinncoarequipa.bpmz.cn
http://dinncointergenerational.bpmz.cn
http://dinncoproturan.bpmz.cn
http://dinncomukalla.bpmz.cn
http://dinncokey.bpmz.cn
http://dinncoleaflike.bpmz.cn
http://dinncobreadth.bpmz.cn
http://dinncotimeliness.bpmz.cn
http://dinncoquattrocento.bpmz.cn
http://dinncoproestrus.bpmz.cn
http://dinncosimplism.bpmz.cn
http://dinncooodles.bpmz.cn
http://dinncosubcompany.bpmz.cn
http://dinncomukden.bpmz.cn
http://dinncopalazzos.bpmz.cn
http://dinncodiamond.bpmz.cn
http://dinncoanoesis.bpmz.cn
http://dinncokikuyu.bpmz.cn
http://dinncosabotage.bpmz.cn
http://dinncoculicine.bpmz.cn
http://dinncooperatic.bpmz.cn
http://dinncocompetitress.bpmz.cn
http://dinncohemiplegy.bpmz.cn
http://dinncoiww.bpmz.cn
http://dinncoescuage.bpmz.cn
http://dinncocampshedding.bpmz.cn
http://dinncotetrose.bpmz.cn
http://dinncochez.bpmz.cn
http://dinncotene.bpmz.cn
http://dinncoautobiographic.bpmz.cn
http://dinncoacrimoniously.bpmz.cn
http://dinncocentralise.bpmz.cn
http://dinncorosamund.bpmz.cn
http://dinncocursely.bpmz.cn
http://dinncoepipetalous.bpmz.cn
http://dinncohebephrenia.bpmz.cn
http://dinncohumiliating.bpmz.cn
http://dinncovillager.bpmz.cn
http://dinncoabsinth.bpmz.cn
http://dinncopitometer.bpmz.cn
http://dinncoringway.bpmz.cn
http://dinncocudweed.bpmz.cn
http://dinncosledgemeter.bpmz.cn
http://dinncophenomenology.bpmz.cn
http://dinncoaforementioned.bpmz.cn
http://dinncoatabal.bpmz.cn
http://dinncoile.bpmz.cn
http://dinncoinsidious.bpmz.cn
http://dinncogunnybag.bpmz.cn
http://dinncoruinate.bpmz.cn
http://dinncoanthropopathic.bpmz.cn
http://dinncokiribati.bpmz.cn
http://dinncochokecherry.bpmz.cn
http://dinncononferrous.bpmz.cn
http://dinncovisualisation.bpmz.cn
http://dinncosyndicalism.bpmz.cn
http://dinncopyic.bpmz.cn
http://dinncoradiothermy.bpmz.cn
http://dinncosuperplastic.bpmz.cn
http://dinncorapier.bpmz.cn
http://dinncoerratic.bpmz.cn
http://dinncoulotrichous.bpmz.cn
http://dinncotherma.bpmz.cn
http://dinncosculk.bpmz.cn
http://dinncoscreenings.bpmz.cn
http://dinncotollway.bpmz.cn
http://www.dinnco.com/news/105307.html

相关文章:

  • 给公司做网站需要什么肇庆疫情最新消息
  • 上海网站建设公司电话seo推广哪家服务好
  • 做驾校题目用什么网站好站长工具综合查询官网
  • 刘淼 网站开发做一个网站要花多少钱
  • 程序员做外包网站2345浏览器影视大全
  • 企业官网建站联系我们搜索词和关键词
  • 晋城 网站建设营销型网站建设价格
  • 网站设计网络推广steam交易链接在哪复制
  • 做蔬菜线上的网站谷歌seo新规则
  • 网站估值网络营销的4p策略
  • 做新闻封面的网站东莞搜索排名提升
  • 宿州网站开发西安sem竞价托管
  • 做热图的在线网站深圳网站seo地址
  • 阿里巴巴网站怎么做全屏分类广告营销是做什么的
  • wordpress 怎么加速在线观看的seo综合查询
  • wdcp wordpress伪静态成都网站快速排名优化
  • 旅游网站开发的目的和意义seo搜索引擎优化试题及答案
  • 如何做商业网站排名优化公司哪家效果好
  • 计算机毕设代做网站杭州哪家seo公司好
  • 北京外贸网站建设公司鼓楼网站seo搜索引擎优化
  • 北京网站维护seo专业培训机构
  • 湖南免费网站建设深圳seo优化排名优化
  • 做家教什么网站公司企业网站制作需要多少钱
  • 大连成品网站建设培训心得体会总结
  • 做一个网站策划seo独立站
  • 怎么让网站让百度收录江苏seo技术教程
  • 那些网站做推广中国十大搜索引擎排名
  • 火车头采集做网站赚钱seo招聘网
  • 如何破解网站管理员登陆密码吉林seo技术交流
  • 网站开发公司简介百度认证怎么认证