node笔记 | 青训营笔记

67 阅读3分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 7 天

React 16.8+的生命周期分为三个阶段,分别是挂载阶段、更新阶段、卸载阶段。

解决res返回中文时乱码的问题

const http = require('http')
const server = http.createServer()
server.on('request', (req, res) => {
    const url = req.url
    const method = req.method
    const str = `请求的地址是${url},请求的方式是 ${method}`
        //调用res.setHeader()方法,设置Content-type响应头,解决中文乱码问题
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
        //调用res.end()向客户端响应一些内容
    res.end(str)
})
server.listen(8080, () => {
    console.log('server running at http://127.0.0.1:8080')
})

根据不同的url返回不同内容

  1. 获取请求的url地址

  2. 设置默认响应内容

  3. 判断用户请求是什么

  4. 设置content-type响应头

  5. 使用res.end()

    let url=....
    if(url===){
    
    }
    

    ====

模块化

概念

  • 是模块化是指解决一个复杂问题时,自顶向下逐层把系统划分成若干模块的过程,对于整个系统来说,模块是可组合 分解和更换的单元

好处

  • 提高代码的复用性
  • 提高了代码的可维护性
  • 可以实现按需加载

node.js

  • 内置模块 fs path http
  • 自定义模块 用户创建的js
  • const custom=require(./custom.js)
    
  • 第三方模块 由第三方开发出来的模块 并非官方提供出来的

向外共享模块作用域中的成员

module

  • 在每个.js模块中都有一个module对象,他里面存储了和当前模块有关的信息
  • 在自定义模块中,可以使用module.exports对象,将模块内的成员共享出去,供外界使用
  • module.exports
  • 在自定义模块中
  • module.exports.username='zs'
    module.exports.sayhello=function(){
    console.log('hello')
    }
    
    以上为自定义模块
    一下调用
    
    const m=require('./自定义模块')
    console.log(m)
    
  • exports跟module.exports一样

npm与包

  • 第三方模块又叫包

  • /*
    npm i moment@版本号
    版本号2.23.0
    第一位是大版本
    第二位是功能版本
    第三位是bug修复
    */
    
  • /*
    npm init -y
    在执行命令处的目录中,快速新建package.json文件
    项目文件名必须是英文 不能出现空格 
    
    配置文件中的dependencies节点
    专门记录npm
    */
    

    一次性安装所有的包

    /*npm i
    读取package.json文件 然后安装所有包
    */
    
    /*
    卸载包
    npm uninstall
    */
    

devDependencies节点

  • 如果某些包只在项目开发阶段会用到,在项目上线之后不会用到
  • /*npm i 包名 -D*/
    
  • 如果都需要用到 把这些包记录到dependencies节点中

nrm工具

  • 更加方便的切换下包的镜像源 利用nrm提供的终端命令,可以快速查看和切换下包的镜像源
  • /*
    npm i nrm -g
    nrm ls 查看所有镜像源
    nrm use taobao
    */
    

项目包

  • 那些被安装在项目的node_modules目录中的包 都是项目包
  • 分为两类开发依赖包 记录到Devdependencies中的 只在开发期间会用到
  • 核心依赖包 被记录到dependencies节点中的包 在开发期间和项目上线后都会用到

i5ting_toc

/*i5ting_toc-f 要转换的md文件路径 -O
把md文件转换为html
*/

开发属于自己的包

function dateFormat(dtStr) {
    const dt = new Date(dtStr)
    const y = padzero(dt.getUTCFullYear())
    const m = padzero(dt.getMonth() + 1)
    const n = padzero(dt.getDate())
    const hh = padzero(dt.getHours())
    const mm = padzero(dt.getMinutes())
    const ss = padzero(dt.getSeconds())

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

function padzero(n) {
    return n > 9 ? n : '0' + n
}
//定义转移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(htmlstr) {
    return htmlstr.replace(/&lt;&quot;|&amp;/g, (match) => {
        switch (match) {
            case '&lt;':
                return '<'

            case '&gt;;':
                return '>'
            case '&quot;':
                return '&quot;'
            case '&':
                return '&amp;'
        }
    })
}
module.exports = {
    dateFormat,
    htmlEscape,
    htmlunEscape
}

讲不同功能进行模块化拆分

  1. 将格式化时间的功能,拆分
  2. 将处理字符串的功能 拆分
  3. 在index.js中 导入两个模块 得到需要向外共享的方法
  4. 在index.js中 使用module.exports 把对应的方法共享出去
  • 在发布包的时候 要登录到官方地址 不可以镜像
  • /*登录 npm login*/
    
  • /*发布 npm publish*
    删除 npm unpublish 包名 --force 只可以删除72小时之内的
    /