diff --git "a/\345\274\240\345\206\240\350\261\252/20230213-Node.js\347\254\254\344\270\200\350\257\276.md" "b/\345\274\240\345\206\240\350\261\252/20230213-Node.js\347\254\254\344\270\200\350\257\276.md" new file mode 100644 index 0000000000000000000000000000000000000000..b152a9e8023d8e3303019fe1fed123f9966d98ca --- /dev/null +++ "b/\345\274\240\345\206\240\350\261\252/20230213-Node.js\347\254\254\344\270\200\350\257\276.md" @@ -0,0 +1,13 @@ +# 2.13笔记 +## Node.js +它是一套JavaScript运行环境 +## npm +Node.js 包管理器,可以下载包、安装包、卸载包、更新包、上传包等 +## nvm +Node.js 版本管理器,可以简单操作node版本的切换、安装、查看等 +## Node.js安装配置 +进入官网下载18.14.0 LTS 长期支持版本 +### 查看是否安装完成及版本 +cmd中输入node -v查看(v是verrsion的简化) + + diff --git "a/\345\274\240\345\206\240\350\261\252/20230215-Node.js\346\250\241\345\235\2271.md" "b/\345\274\240\345\206\240\350\261\252/20230215-Node.js\346\250\241\345\235\2271.md" new file mode 100644 index 0000000000000000000000000000000000000000..47561a31148b2ab9a666461056bdbd3f2f8dd06c --- /dev/null +++ "b/\345\274\240\345\206\240\350\261\252/20230215-Node.js\346\250\241\345\235\2271.md" @@ -0,0 +1,43 @@ +# 2.15笔记 +## 绝对路径与相对路径 +### 绝对路径 +绝对路径就是完整的文件地址,通常是指两个文件存在引用关系的时候的一种路径表达,采用的完整路径方式 +### 相对路径 +通常是指两个文件存在引用关系的时候的一种路径表达,采用的简洁方式 +## 命令模式 +node index.js执行就是命令模式 +## 交互模式 +输入node就是进入交互模式 +## 退出终端 +Ctrl+C + +exit +## 手写调试配置文件 +```js +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch", + "program":"${workspaceFolder}/index.js" //调试入口 + } + ] +} +``` +## 模块 +在Node环境中,一个.js文件就称之为一个模块(module) + +- 降低代码的冗长度 +- 提高代码的可维护性 +- 使得编写代码不必从零开始 +- 避免函数名与变量名冲突 +### 引用模块 +modele.exports = add + +对外输出变量,将add作为变量进行传输 + +let add = require ('./xxx') + +引入其他模块输出的对象 \ No newline at end of file diff --git "a/\345\274\240\345\206\240\350\261\252/20230216-Node.js\346\250\241\345\235\2272.md" "b/\345\274\240\345\206\240\350\261\252/20230216-Node.js\346\250\241\345\235\2272.md" new file mode 100644 index 0000000000000000000000000000000000000000..cb88046ae0dc76fcca313f4513ec13cdf3e508cb --- /dev/null +++ "b/\345\274\240\345\206\240\350\261\252/20230216-Node.js\346\250\241\345\235\2272.md" @@ -0,0 +1,58 @@ +# 2.16笔记 +## 基本模块 +### global +Node.js环境中的唯一全局对象 +### Process +Node.js提供的一个对象,代表当前Node.js进程 +## 文件系统模块(fs) +Node.js内置的fs模块,负责读写文件。同时提供异步和同步方法 +### 异步读取文件 +异步是通过回调来处理 +```JS +//引入文件模块 +const fs = require ('fs'); +//使用readFile异步读的方法来读取文件 +fs.readFile('文件路径','utf-8',(err.date)=>{ + if(err){ + console.log(err) + } +})else{ + console.log(date); +} +console.log('读取成功!'); +``` +异步读取时不能保证读取顺序。有两方法解决: + +1.使用peomess + +2.使用同步读取文件 +### 同步读取文件 +```JS +//引入文件模块 +const fs = require ('fs'); +//使用readFileSync同步读的方法来读取文件 +let re1 = fs.readFileSync('文件路径','utf-8') +console.log(re1); +let re2 = fs.readFileSync('文件路径','utf-8') +console.log(re2); +console.log('读取成功!'); +``` +Node.js环境中使用异步较多,因为Node.js是单线程的 +### 异步写入文件 +```JS +//引入文件模块 +const fs = require ('fs'); +//使用writeFile异步写的方法来写入文件 +fs.writeFile('文件路径','写入的内容',()=>{ + console.log('写入成功'); +}) +``` +### 同步写入文件 +```JS +//引入文件模块 +const fs = require ('fs'); +//使用writeFileSync同步写的方法来写入文件 +fs.writeFileSync('文件路径','写入的内容',()=>{ + console.log('写入成功'); +}) +``` \ No newline at end of file diff --git "a/\345\274\240\345\206\240\350\261\252/20230217-Node.js\346\250\241\345\235\2273.md" "b/\345\274\240\345\206\240\350\261\252/20230217-Node.js\346\250\241\345\235\2273.md" new file mode 100644 index 0000000000000000000000000000000000000000..35eeb29a9144902ad83a44bce19be22027aa03c7 --- /dev/null +++ "b/\345\274\240\345\206\240\350\261\252/20230217-Node.js\346\250\241\345\235\2273.md" @@ -0,0 +1,36 @@ +# 2.17笔记 +## Stream(流)模块 +## crypto模块 +## HTTP模块 +HTTP核心模块是Node.js网络的关键模块 +## HTTP协议 +1.请求/响应式的协议 + +2.常见方法有:get、post、delete、options + +3.默认端口是80 + +4.默认端口的http式不安全的,是明文的 + +5.后面新增加的https是安全的,是加密的 + +6.websocket,简称ws。它是即时通讯协议,也是基于http协议 + +ws:服务端主动向客户端发送 + +客户端向服务端请求request + +服务端向客户端响应respose +```JS +//引入文件模块 +let http = require('http'); +//使用creatServer创建http服务 +let server = http.createServer((req,res)=>{ + res.end('hello,world!'); + +}) + +server.listen(8080); + +console.log('请求成功!'); +``` \ No newline at end of file