[Node]node.js实现登录注册

背景

补充学习下node知识,课程来自imooc。个人学习笔记,如有侵权,会删除。提示直接从http模块看起。重点看案例。

node课程

笔记

node基础

  1. node介绍
    • 脱离浏览器运行JS
    • 后台API编写
    • Webpack,gulp,Npm等等
    • 中间层:服务器中负责IO读写的中间层服务器
  2. node 可以
    • 优化性能
    • 异步io
    • 处理数据
    • 安全性
  3. 案例login,signup

node介绍

  1. node优势
    • 便于前端开发入门
    • 性能高
    • 利于前端代码整合
  2. 环境搭建
    • 官网下载
    • node -v检验
    • node 1.js
    • 清屏幕 cls
  3. 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模块

  1. 全局模块 随时可以访问,无需引用
    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
    
  2. 系统模块 , 需要require(),但是不需要单独下载

    • path处理文件路径和目录路径的实用工具
    1. fs推荐异步推荐 在这里插入图片描述
  3. 自定义模块

    • exports
      mod.js
      export.a=1
      export.b=1
      index.js
      const  mod1=require('./mod')
      console.log(mod1.a)
      
    • modules
      在这里插入图片描述
    • require
      – 如果有路径,就去路径找
      – 没有就去node_modules找
      – 再去node的安装目录里面找
  4. http模块
    node作为web服务器
    服务器对象:http.createServer()
    request(req),response(res)

    1. 在这里插入图片描述
      在这里插入图片描述

    2. 执行 node index

    3. 打开url http://localhost:8888

    4. 打开url http://localhost:8888/1.html

    5. 总结
      监听8888端口,访问8888/1-http.html,如
      果访问成功,返回1-http.html的内容,如果失败,会返回404 not found。

数据交互

浏览器发送请求给服务器,服务器收到命令后,把得到的数据返回给浏览器,进行交互。
中间是报文,有头和体
在这里插入图片描述

get

  1. get 获取数据,放在url里面进行传输,传文字,数字,不能视频和图片
  2. doc可以在vscode里面快速创建.html文件
  3. 例子
  • 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

在这里插入图片描述

  1. buffer把拿到的数据二进制
  2. 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>

在这里插入图片描述

案例

  1. 接口
  • 不同功能层之间的通信规则为接口
  • node接口五要素
    接口
    参数
    返回值
    请求方式
    编写接口文档
    以上面为例子
    完整的请求路径:"http://localhost:8899/aaa?username=10328&password=1"
    接口:/aaa
    请求参数:username=10328&password=1
    
  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>
  • 1
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值