nodejs 发布包-轻松拥有属于自己的包itheima-tools
web nodejs 2023-01-12 716 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6413405 2611 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)
什么是包?
Node.js 中的第三方模块又叫做包

为什么需要包?
由于 Node.js 的内置模块仅提供了一些底层的 API,导致在基于内置模块进行项目开发的时,效率很低

包是基于内置模块封装出来的,提供了更高级、更方便的 API,极大的提高了开发效率

包和内置模块之间的关系,类似于 jQuery 和 浏览器内置 API 之间的关系

包的分类:
项目包: 用require引入使用,被安装到项目的 node_modules 目录中的包.

项目包分为:

1.开发依赖包 :只在开发期间会用到 ;记录在devDependencies 节点

2.核心依赖包(生产依赖包):在开发期间和项目上线之后都会用到;记录在dependencies 节点

全局包: 通过命令行的方式存在,只有工具性质的包,才有全局安装的必要性。

npm root -g  // 查看包安装在哪里,如何被使用
接下来,我们来开发属于自己的包.实现的功能有 格式化日期,转移 HTML 中的特殊字符,还原 HTML 中的特殊字符.

轻松拥有属于自己的包
一个规范的包,它的组成结构,必须符合以下 3 点要求:

1.必须以单独的目录而存在(一个包一个文件夹);

2.包的顶级目录下要必须包含 package.json;

package.json中必须包含name(名字),version(版本号),main` (包的入口) 这三个属性;

第一步:初始化包的基础结构
新建 itheima-tools 文件夹,作为包的根目录

在 itheima-tools 文件夹中,新建如下三个文件:

package.json (包管理配置文件)

index.js (包的入口文件)

README.md (包的说明文档)

第二步:初始化 package.json 配置文件
npm init -y 
// 快速创建package.json

第三步:在 index.js 中定义要实现的功能的方法

// 包的入口文件 index.js

// 定义格式化时间的函数
function dateFormat (dateStr) {
  const dt = new Date(dateStr)

  const y = padZero(dt.getFullYear())
  const m = padZero(dt.getMonth() + 1)
  const d = padZero(dt.getDate())

  const hh = padZero(dt.getHours())
  const mm = padZero(dt.getMinutes())
  const ss = padZero(dt.getSeconds())

  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}

// 定义一个补零的函数
function padZero (n) {
  return n > 9 ? n : '0' + n
}

// index.js
// 定义转义 HTML 字符的函数
function htmlEscape(htmlstr) {
  return htmlstr.replace(/<|>|"|&/g, match => {
    switch (match) {
      case '<':
        return '&lt;'
      case '>':
        return '&gt;'
      case '"':
        return '&quot;'
      case '&':
        return '&amp;'
    }
  })
}
// 定义还原 HTML 字符的函数
function htmlUnEscape(str) {
  return str.replace(/&glt;|&gt;|&quot;|&amp;/g, (match) => {
    switch (match) {
      case '&glt;':
        return '<'
      case '&gt;':
        return '>'
      case '&quot;':
        return '"'
      case '&amp;':
        return '&'
    }
  })
}

// 向外暴露需要的成员
module.exports = {
  dateFormat,htmlEscape,htmlUnEscape
}
测试代码是否正常运行

const itheima = require('./itheima-tools')
const dt = new Date()

console.log(itheima.dateFormat(dt));

const str = '<h2 style="color:red;">这是标题2</h2>'
console.log(itheima.htmlEscape(str));

const htmlStr = '&lt;h2 style=&quot;color:red;&quot;&gt;这是标题2&lt;/h2&gt;'
console.log(itheima.htmlUnEscape(htmlStr));
第四步:划分不同的模块
1,将格式化时间的功能,拆分到 src -> dateFormat.js 中
function dateFormat(dateStr){
  const dt = new Date(dateStr)

  const y = dt.getFullYear()
  const m = padZero(dt.getMonth() + 1)
  const d = padZero(dt.getDate())

  const hh = padZero(dt.getHours())
  const mm = padZero(dt.getMinutes())
  const ss = padZero(dt.getSeconds())

  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}
// 自动补0
function padZero(n){
  return n > 9 ? n : '0' + n
}

module.exports = {
  dateFormat
}

2,将处理 HTML 字符串的功能,拆分到 src -> htmlEscape.js 中
// 转义字符
function htmlEscape(htmlStr){
  return htmlStr.replace(/<|>|"|&/g,(match)=>{
    switch(match){
      case '<':
        return '&lt;'
      case '>':
        return '&gt;'
      case '"':
        return '&quot;'
      case '&':
        return '&amp;'
    }
  })
}
// 定义还原html字符的函数
function htmlUnEscape(str){
  return str.replace(/&lt;|&gt;|&quot;|&amp;/g,(match)=>{
    switch(match){
      case '&lt;':
        return '<'
      case '&gt;':
        return '>'
      case '&quot;':
        return '"'
      case '&amp;':
        return '&'
    }
  })
}

module.exports = {
  htmlEscape,htmlUnEscape
}

3,在 index.js 中,导入两个模块,得到需要向外共享的方法
在 index.js 中,使用 module.exports 把对应的方法共享出去

const date = require('./src/dateFormat')
const escape = require('./src/htmlEscape')

module.exports = {
  ...date,
  ...escape
}

第五步:编写包的说明文档
README 文件中具体写什么内容,没有强制性的要求;只要能够清晰地把包的作用、用法、注意事项等描述清楚即可

我们现在所创建的这个包的 README.md文档中,会包含以下 6 项内容

安装方式

导入方式

格式化时间

转义 HTML 中的特殊字符

还原 HTML 中的特殊字符

开源协议
### 安装
```
npm i flightloong-tools
```

### 导入
```js
const itheima = require('./flightloong-tools')
```

### 格式化时间
```js
// 调用 dateFormat 对时间进行格式化
const dtStr = itheima.dateFormat(new Date())
// 结果  2020-04-03 17:20:58
console.log(dtStr)
```

### 转义 HTML 中的特殊字符
```js
// 带转换的 HTML 字符串
const htmlStr = '<h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>'
// 调用 htmlEscape 方法进行转换
const str = itheima.htmlEscape(htmlStr)
// 转换的结果 &lt;h1 title=&quot;abc&quot;&gt;这是h1标签&lt;span&gt;123&amp;nbsp;&lt;/span&gt;&lt;/h1&gt;
console.log(str)
```

### 还原 HTML 中的特殊字符
```js
// 待还原的 HTML 字符串
const str2 = itheima.htmlUnEscape(str)
// 输出的结果 <h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>
console.log(str2)
```

### 开源协议
ISC
第六步:注册 npm 账号(已经有npm账号可以跳过这一步)
访问 (npm ) 网站,点击 sign up 按钮,进入注册用户界面

填写账号相关的信息:Full Name、Public Email、Username、Password

点击 Create an Account 按钮,注册账号

登录邮箱,点击验证链接,进行账号的验证

第七步:登录 npm 账号
注意:在运行 npm login 命令之前,必须先把下包的服务器地址切换为 npm 的官方服务器。否则会导致发布包失败!
// 查看当前镜像地址
nrm ls 
// 切换到npm 
nrm use npm
npm 账号注册完成后,可以在终端中执行 npm login 命令,依次输入用户名、密码、邮箱后,即可登录成功

第八步: 把包发布到 npm上
将终端切换到包的根目录之后,运行 npm publish 命令,即可将包发布到 npm 上(注意:包名不能雷同)

拓展: 删除已发布的包

运行 下面的命令,即可从 npm 删除已发布的包

npm unpublish 包名 --force 
注意事项

npm unpublish 命令只能删除 72 小时以内发布的包

npm unpublish 删除的包,在 24 小时内不允许重复发布

发布包的时候要慎重,尽量不要往 npm 上发布没有意义的包!

版权声明:本篇文章来源于网络。 来源链接

相关推荐
windows如何把已安装的nodejs高版本降级为低版本
nodejs | 2022-10-17 2063
第一步:先清空本地安装的node.js版本 1.按健win+R弹出窗口,键盘输入cmd,然后敲回车,然后进入命令控制行窗口,并输入where node查看之前本地安装的node的路径 2.找到上面找到的路径,将node.exe所在的父目录里面的所有东西都删除 3.为了彻底删除之前安装的node.js,鼠...
This application is only supported on Windows 8.1, Windows Server 2012 R2, or hi gher.
nodejs | 2022-07-06 1788
有的nodejs版本不支持win7,在win7系统中执行npm -v时会有以下提示 This application is only supported on Windows 8.1, Windows Server 2012 R2, or hi gher. 下载v12.16.2及之前的版本即可 https://nodejs.org/d...
评论:0条
评论加载中...
发表评论