背景
补充学习下node知识,课程来自imooc。个人学习笔记,如有侵权,会删除。提示直接从http模块看起。重点看案例。
笔记
node基础
- node介绍
- 脱离浏览器运行JS
- 后台API编写
- Webpack,gulp,Npm等等
- 中间层:服务器中负责IO读写的中间层服务器
- node 可以
- 优化性能
- 异步io
- 处理数据
- 安全性
- 案例login,signup
node介绍
- node优势
- 便于前端开发入门
- 性能高
- 利于前端代码整合
- 环境搭建
- 官网下载
- node -v检验
- node 1.js
- 清屏幕 cls
- npm包管理(引用第三方仓库)
- npm init生成package.js
- package.js管理项目插件
- npm cnpm
- npm install xxxx 安装插件,出现在package.json 的dependencies里面,以及node_modules文件架
- 简写 npm i xxx
- npm uninstall xxx 删除插件
- npm un xxx
- cnpm 淘宝镜像
- cnpm i xxx
- cnpm un xxx
- 常用命令
- npm unpdate xxx
- npm i //把package里面依赖装上
node模块
-
全局模块 随时可以访问,无需引用
process.env 环境变量
process.argv
– parseInt转化为数字
– 输入node index 传参数let num1=parseInt(process.argv[2]) let num2=parseInt(process.argv[3]) console.log(num1,num2) //cmd里面输入 -node index 11 22 - 33
-
系统模块 , 需要require(),但是不需要单独下载
- path处理文件路径和目录路径的实用工具
- fs推荐异步
- path处理文件路径和目录路径的实用工具
-
自定义模块
- exports
mod.js export.a=1 export.b=1 index.js const mod1=require('./mod') console.log(mod1.a)
- modules
- require
– 如果有路径,就去路径找
– 没有就去node_modules找
– 再去node的安装目录里面找
- exports
-
http模块
node作为web服务器
服务器对象:http.createServer()
request(req),response(res)-
-
执行 node index
-
打开url http://localhost:8888
-
打开url http://localhost:8888/1.html
-
总结
监听8888端口,访问8888/1-http.html,如
果访问成功,返回1-http.html的内容,如果失败,会返回404 not found。
-
数据交互
浏览器发送请求给服务器,服务器收到命令后,把得到的数据返回给浏览器,进行交互。
中间是报文,有头和体
get
- get 获取数据,放在url里面进行传输,传文字,数字,不能视频和图片
- doc可以在vscode里面快速创建.html文件
- 例子
- action后面地址表示,你要把form表单数据提交的地址,一般由后台提供,后台接口,方法get
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="http://localhost:8888/aaa" method="get">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
let http = require('http')
let url = require('url')
http.createServer((req, res) => {
//console.log(req.url);
//console.log(url.parse(req.url, true));
//转json
let { pathname, query } = url.parse(req.url, true);
console.log(pathname, query);
}).listen(8890)
let { pathname, query } = url.parse(req.url, true);
//return
/aaa [Object: null prototype] { username: '123', password: '333' }
post
- buffer把拿到的数据二进制
- end结束事件
let http = require('http')
let querystring = require('querystring')
http.createServer((req, res) => {
console.log("3-post");
let result = []
req.on('data', buffer => {
result.push(buffer)
})
req.on('end', () => {
let data = Buffer.concat(result).toString()
//console.log(data.toString());
//转json
console.log(querystring.parse(data));
})
}).listen(8891)
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="http://localhost:8891/bbb" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
案例
- 接口
- 不同功能层之间的通信规则为接口
- node接口五要素
接口
参数
返回值
请求方式
编写接口文档
以上面为例子完整的请求路径:"http://localhost:8899/aaa?username=10328&password=1" 接口:/aaa 请求参数:username=10328&password=1
- 登录注册
- 后台
const http = require('http')
const url = require('url')
const querystring = require('querystring')
const fs = require('fs')
let user = {
admin: 123456
}
http.createServer((req, res) => {
let path, get, post
console.log("req.method " + req.method);
if (req.method == 'GET') {
let { pathname, query } = url.parse(req.url, true)
path = pathname;
get = query;
console.log("pathname, query=" + pathname, query)
complete()
} else
if (req.method == 'POST') {
let arr = []
path = req.url;
req.on('data', buffer => {
arr.push(buffer)
})
req.on('end', () => {
let post = querystring.parse(Buffer.concat(arr).toString());
console.log(post)
complete(post)
})
}
function complete(post) {
console.log('path=' + path)
if (path === '/login') {
let { username, password } = get
//乱码问题
res.writeHead(200, {
"Content-Type": "text/plain;charset=utf-8"
})
console.log(username, password)
if (!user[username]) {
res.end(JSON.stringify({
err: 1,
msg: "no exist"
}))
} else if (user[username] != password) {
res.end(JSON.stringify({
err: 1,
msg: "error password中文"
}))
} else {
res.end(JSON.stringify({
err: 0,
msg: "succeed"
}))
}
}
else if (path === '/reg') {
res.writeHead(200, {
"Content-Type": "text/plain;charset=utf-8"
})
console.log(post);
let { username, password } = post
if (user[username]) {
res.end(JSON.stringify({
err: 1,
msg: "账户 exist"
}))
} else {
user[username] = password
res.end(JSON.stringify({
err: 0,
msg: "注册成功"
}))
}
}
else {
fs.readFile(`./${req.url}`, (err, data) => {
if (err) {
res.writeHead(404)
res.end('404 not found')
} else {
res.end(data)
}
})
}
}
}
).listen(8898)
- 前台
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>恭喜登录成功!!!</div>
</body>
</html>