logo头像

前端从未如此简单有趣

关于Node的基本认识【BASIC】

1
可以脱离浏览器来执行js代码,没有DOM和BOM对象,针对后端可以编写接口,提供网页资源,前端可以集成各种工具(承上启下)

node

1.Buffer

image-20240722185800133

2.Buffer相关操作

image-20240722185811863

1
2
3
4
5
6
let buf = Buffer.alloc(10)
console.log(buf);
let buf_2 = Buffer.allocUnsafe(100)
console.log(buf_2);
let buf_3 = Buffer.from('hello')
console.log(buf_3);

3.fs模块-读写模块【回顾一下liunx操作系统】【增删改查操作】

image-20240722185823072

1
与本机文件系统进行交互的,方法和属性

image-20240722185832249
image-20240722185840006

4.fs异步和同步

image-20240722185852184

5.fs文件追加操作

image-20240722185903793

6.文件写入的场景-【做持久化】

image-20240722185915521

7.文件读取

image-20240722185929153

8.写一个创建文件的脚本

9.fs流式写入

image-20240722185950220

10.exports对象

1
2
由于module.exports 单词写起来比较复杂,为了简化向外共享成员的代码,Node 提供了exports 对象。默认情况下,exports和module.exports 指向同一个对象。最终共享的结果,还是以module.exports指向的对象为准。
时刻谨记,require()模块时,得到的永远是module.exports指向的对象:

11.计算机组成

image-20240722190005679

12.模块化

1
2
3
4
5
-require的使用
-模块化的好处->无法共享成员
-module对象,每个.js都自定义模块中都有一个module对象
-exports 自定义模块中,可以使用exports将模块中的成员共享出去,给外界使用 在自定义模块中,默认情况下,module.export = {}
使用require()方法导入模块时,导入的结果,永远以module.exports指向的对象为准。

image-20240722190016626

13.模块化初体验

image-20240722190034603

14.模块暴露数据

image.png
image-20240722190048595

15.导入(引入)模块

image-20240722191730313
导入 js 和 json文件时 可以省略后缀

16.导入模块的基本流程

image-20240722191741753

17.commonJS规范

image-20240722191752584

18.包管理工具【哆啦A梦】

image-20240722192635496

19.npm

image-20240722192646803

20.npm基本使用

20.1初始化

image-20240722192726796

1
2
3
4
5
6
7
8
9
10
11
12
{
"name": "test01",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "c",
"license": "ISC"
}

image-20240722192737910

20.2如何搜索/下载npm包 npmjs.com

image-20240722192802505

20.3require导入包的基本流程

image-20240722192812083

20.4开发依赖和生产依赖

https://juejin.cn/post/7135795969370619918
image.png

20.5npm全局安装

image-20240722192854948

20.6修改windows执行策略

image-20240722192922707
image.png

21.环境变量path

image-20240722192936895

20.1 npm安装包的所有依赖

image-20240722192946860

22.安装指定版本的包

image-20240722192954600

23.配置命令别名

image-20240722193005553

24.cnpm

image-20240722193017454

25.配置淘宝镜像

image-20240722193026238

26.yarn

image-20240722193035283

27.npm和yarn的使用

image-20240722193128589

28.管理发布包

image-20240722193139825

29.更新和删除

image-20240722193152416

30.扩展

image-20240722193307073

31.nvm

image-20240722193315354

32.常用命令

image-20240722193323474

33.expressJS

Express是一个流行的Node.js Web应用程序框架,用于构建服务器端应用程序。它提供了一组简洁而灵活的工具和中间件,使开发者可以轻松地构建RESTful API和Web应用。Express具有路由、请求和响应处理、中间件等功能,可以帮助开发者管理HTTP请求和构建服务器端逻辑。通过Express,可以搭建一个响应请求的服务器,处理前端发起的HTTP请求并返回相应的数据或页面。

34.简单应用

35.路由

image-20240722193340649

  • 方法
  • 路径
  • 回调
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    const  express = require('express')

    const app = express()

    // 路由
    app.get('/home',(req,res) =>{
    res.end('Welcome to the Home page')
    } )

    app.get('/',(req,res) =>{
    res.end('Welcome to the Home page with instructions for creating')
    })

    app.post('/login',(req,res) =>{
    res.end('Welcome to the Login page')
    })

    app.get('*',(req,res) =>{
    res.end('404 Not Found ')
    })

    app.listen(3000,()=>{
    console.log('listening on http://localhost')
    })

36.获取请求参数

image-20240722193356905

37.获取路由参数

image-20240722193743893