day33
一、模块化
1.node模块化
let listMode = require('./modules/list')let {index, tab} = require('./modules/tab')
// console.log(listMode)// console.log(tabMode)
// console.log(listMode.index)// console.log(tabMode.index)
// listMode.list()
// console.log(index)// tab()
2.path模块
// path内置模块,专门用来处理路径
const path = require('path')
// 获取路径最后一部内容,一般用它来获取文件名称
// const result = path.basename('day13/code/modules/list.js')
// const result = path.basename('day13/code/pages/index.html')
// console.log(result)
// 给定的路径连接在一起,可以把路径片段拼成完整路径
// const result = path.join('day13', 'code', '03_path模块.js')
// const result = path.join('day13', 'code', '../','03_path模块.js')
// console.log(result)
// 读取绝对路径,直接从根路径下读取的
// const result = path.resolve(__dirname, 'day13', 'code')
// console.log(result)
3.url模块
// url内置模块,可以把url地址里面所有的东西转成对象,方便后面的使用const url = require('url')
const href = 'http://www.xxx.com:8080/pathname?id=100#bbb'
// const result = url.parse(href, true)// console.log(result.query)
const { query, protocol } = url.parse(href, true)console.log(query.id, protocol)
4.querystring模块
// querystring内置模块,可以把查询字符串转成对象,把对象转成查询字符串
const querystring = require('querystring')
// console.log(querystring.stringify({a:1, b: 2}))
// console.log(querystring.parse('a=1&b=2'))
// 把对象转查询字符串
console.log(querystring.encode({a:1, b: 2}))
// 把查询字符串转对象
console.log(querystring.decode('a=1&b=2'))
5.fs模块
// fs内置模块,读取和写入文件内容
const fs = require('fs')
// 异步读取
// 参数1表示的是读取文件的路径
// 参数2表示的是字符串编码
// 参数3表示的一个回调函数,err表示的错误信息,res表示的是结果
// fs.readFile('./data/hello.txt', 'utf-8', (err, res)=>{
// console.log(res)
// })
// const res = fs.readFileSync('./data/hello.txt', 'utf-8')
// console.log(res)
// 异步写入
// fs.writeFile('./data/hello.txt', '哈哈,没有代码提示真爽!', ()=>{
// console.log('写入成功')
// })
// 同步写入
// fs.writeFileSync('./data/hello.txt', '嘿嘿')
fs.readFile('./pages/list.html', 'utf-8', (err, res)=>{console.log(res)
})
6.hppt模块
// http模块,可以用来搭建后端的服务器,可以用来开发接口
const http = require('http')
// 访问服务器地址:127.0.0.1:端口号 localhost:端口号
// 创建一个服务
const server = http.createServer((request, response)=>{// 请求地址// console.log(request.url)// 请求类型// console.log(request.method)// 请求头信息// console.log(request.headers)
// 设置字符串编码response.writeHead(200, {'Content-type':'text/html; charset=utf-8'})// res.write() 可以给前端返回数据,可以使用多个// response.write('hello,我是node服务器,你可以使用我了!')// response.write('嘿嘿')// 注意点:最终返回完毕后,还需要终止服务// res.end() 其实也可以给前端返回数据,所以一般使用它比较多// response.end()
// response.end('{"name": "张涛}')
// 复杂版本开发api接口,前端根据这个接口让后端返回需要的数据switch(request.url.split('?')[0]){case '/list':response.end('这是一个列表接口')breakcase '/cart':response.end('这是一个购物车接口')breakcase '/login':response.end('这是一个登录接口')default:response.end('not found')}
})
// 监听端口号
server.listen(2402, ()=>{console.log('您的服务器已经开启!')
})
二、npm
npm包管理器+ + 常用的插件、类库、框架都进行了集中管理,不用去各个的官网挨个进行下载,直接在npm里面可以下载+ npm依赖的是node环境,没有node环境是单独使用不了包管理器+ npm管理文件是以命名行的形式进行的+ 直接可以在终端里面通过一些专门的命令来对文件的下载、删除进行操作npm常用命令+ npm init -y => 表示项目初始化=> 当执行了这句命令后,表示你整个项目都可以npm包管理器进行管理=> 当项目初始化成功后,项目目录里面会自动生成一个package.json的配置文件,这个文件里面有你项目的基本信息,例如:项目名称、版本号、项目启动命名等等+ npm install 包@版本号=> install表示安装,可以简化成i=> 包表示的就是你需要下载的文件=> @后面跟的是文件的版本号=> 如果包名后面没有跟版本后,默认下载的是最新版本的=> 使用了npm install后会默认在项目文件夹里面生成一个node_modules文件夹=> node_modules文件夹专门用来存放你下载的包+ npm uninstall 包@版本号=> uninstall表示安装,可以简化成un=> 表示删除下载的包文件+ npm i || npm install=> 当咱们把node_modules文件夹删除后=> 执行这个命名,可以根据package.json文件里面的记录恢复node_modules文件夹pnpm+ 在npm的基础上衍生而来的一个包管理器+ 它的速度会更快+ 项目初始化的时候还是得使用npm init -y+ 下载文件的时候把之前的npm换成pnpm即可
三、RESTful
RESTful 风格是一种设计 Web API 的方式,它让 API 更加直观、易于理解和使用
获取所有文章列表
GET /articles
获取单个文章详情
GET /articles/123 表示获取ID为123的文章
注册新用户
POST /users
{"username": "jane_doe","email": "dw@example.com","password": "dw@ssw0rd"
}
修改用户信息
PUT /users/jane_doe
{"email": "jane.doe.new@example.com","password": "newP@ssw0rd"
}
删除用户账户
DELETE /users/jane_doe
RESTful 风格的例子中:
-
URL 直接表示资源(如
/articles表示文章集合,/articles/123表示ID为123的文章),且使用标准HTTP动词(GET、POST、PUT、DELETE)来表示对资源的操作。 -
请求参数通常包含在请求体(POST、PUT)或直接作为URL路径的一部分(GET、DELETE),而不是作为查询参数。
不遵循 RESTful 风格
获取所有文章列表 GET /getArticlesList获取用户信息 GET /getUserInfo?username=john_doe删除用户 POST /removeUser
非 RESTful 风格的例子中:
-
URL 表示的是操作而非资源(如
/getArticlesList、/addArticle),且可能使用任何HTTP动词(通常为POST)来执行各种操作。 -
请求参数往往以查询参数(如
?id=123)或请求体中的特定字段(如newTitle、newContent)的形式出现,不直接反映资源本身。
总的来说,RESTful 风格通过清晰的资源模型、标准的HTTP方法和简洁的URL结构,使得API更易于理解和使用,同时也便于自动化工具、浏览器插件等对API进行处理和交互。
