Node.js 简记

阅读 67

2022-01-21

目录

Node.js简记

一、fs文件系统模块

1.导入
const fs = require('fs')
2.读取指定文件中的内容
  • 参数1:path必选参数,表示文件路径
  • 参数2:可选参数,表示以什么编码格式来读文件
  • 参数3:必选参数,文件读取完成以后,通过回调函数拿到读取失败和成功的结果 err dataStr
const fs = require('fs')
fs.readFile('./files/11.txt','utf8',function(err,dataStr){
    //如果读取成功,则err的值为null
    //如果失败,err为错误对象,dataStr为undefined
})
3.向指定文件写入文件内容
  • 参数1:path必选参数,表示文件路径
  • 参数2:写入的内容
  • 参数3:可选参数,表示以什么编码格式来读文件,默认是utf8,可不写
  • 参数4:必选参数,文件读取完成以后,通过回调函数拿到读取失败和成功的结果 err dataStr
const fs = require('fs')
fs.readFile('./files/2.txt','abcd',function(err){
	//如果文件写入成功 err的值为null
    //如果写入失败 err的值为错误对象
    if(err){
        return console.log('文件写入失败'+err.message)
    }
    console.log('文件写入成功')
})

二、path路径模块

1.导入
const path = require('path')
2.路径拼接
  • 参数:可以传递任意多个路径片段
const path = require('path')
//注意 ../会抵消前面一个路径 不加/则会自动添加
const pathStr = path.join('/a','/b/c','../','./d','e')
console.log(pathStr) // \a\b\d\e
3.获取路径中的文件名
  • 参数path必选:表示一个路径的字符串
  • 参数ext可选:表示文件扩展名
  • 返回:表示路径中的最后一部分
const path = require('path')
//注意 ../会抵消前面一个路径 不加/则会自动添加
const fpath = '/a/b/c/index.html'
const fullName = path.basename(fpath)
console.log(fullName) //打印全部文件名,包括后缀名.html

const nameWithoutExt = path.basename(fpath,'.html') //移除扩展名部分
console.log(nameWithoutExt)
4.路径拼接
  • 参数:传递路径
const path = require('path')
//注意 ../会抵消前面一个路径 不加/则会自动添加
const fpath = '/a/b/c/index.html'
const fext = path.extname(fpath)
console.log(fext) // 输出.html

三、http模块

1.导入
const http = require('http')
2.创建一个基本的web服务器
const http = require('http')const server = http.createServer()//为服务器实例绑定request事件server.on('request',function(req,res){    console.log('Someone visit our web server.')})//启动服务器server.listen(8080,function(){    console.log('server running at http://localhost:8080')})
3.req请求对象

req.url是客户端请求的url地址,从端口号开始的地址

req.method是请求的method类型

5.res响应对象

res.end(arg)向客户端发送指定内容,并结束此次请求

6.解决中文乱码问题
const str = `您请求的url地址为${req.url},请求的method为${req.method}`res.setHeader('Content-Type','text/html;charset=utf-8');res.end(str)

四、模块化

1.加载模块
//加载内置模块const http = require('http')//加载自定义模块const custom = require('./custom.js')//加载第三方模块const moment = require('moment')
2.模块作用域
  • 向外共享模块作用域中的成员

    自定义模块.js

    //在一个自定义模块中,默认情况下,module.exports = {}//向module.exports对象上挂载username属性module.exports.username = 'zs'//向module.exports对象上挂在username属性module.exports.sayHello = () => {    console.log('Hello')}
    

    test.js

    //在外界使用require导入一个自定义模块的时候,//得到的成员就是那个模块通过module.exports指向的那个对象const m = require('./自定义模块')console.log(m) //{username:'zs',sayHello:[function]}
    
  • exports对象

  • 注意

    exports.username = 'zs'module.exports = {    gender:'男',    age:20}// require {gender:'男',age:22}
    
    module.exports.username = 'zs'exports = {    gender:'男'    age:20}//最终require得到的对象是{username:'zs'} 因为永远都是module.exports指向的对象
    
    exports.username = 'zs'module.exports.gender = '男'// require: {username:'zs',gender: '男'}
    
    exports = {    gender:'男',    age:20}module.exports = exportsmodule.exports.username = 'zs'//require得到的对象时{gender:'男',age:20,username:'zs'} 与以上推论相符
    
3.模块化规范
  • CommonJS 规范:

    ① 每个模块内部,module变量代表当前模块

    ② module变量是一个对象,他的exports属性是对外的接口

    ③ 加载某个模块,其实是加载该模块的module.exports属性。require()方法用于加载模块

4.npm与包
  • npm初体验

    npm i moment
    
    const moment = require('moment')const dt = moment().format('YYYY-MM-DD HH:mm:ss')console.log(dt)
    
  • install后多地文件夹和文件

  • 安装指定的版本的包

    npm i moment@2.22.2
    

    第一位数字:大版本

    第二位数字:功能版本

    第三位数字:Bug修复版本

    版本号提升规则:只要前面的版本号增长了,则后面版本号归零

五、包管理配置文件

1.快速创建package.json
//在执行命令所处的目录中,快速创建package.json文件npm init -y//项目名称不能有中文和空格
2.dependencies节点
npm install moment jquery art-template
"dependencies": {    "art-template":"^4.13.2",    "jquery":"^3.4.1",    "moment":"^2.24.0"}
3.一次性安装所有的包
//执行 npm install 命令时会读取package.js下dependencies下的所有包//npm包管理工具会一次性全部安装所有包npm install
4.卸载包
npm uninstall moment
5.devDependencies节点
npm i 包名 -D
npm install 包名 --save-dev
npm i webpack -D
"devDependencies": {    "webpack":"^3.4.1"}

精彩评论(0)

0 0 举报