1、封装一个模块,实现加减乘除的功能(计算器)
文件结构:
|——demo
|——node_modules
|——Q_add
|——index.js
|——test.js
index.js
//传入两个数字,返回加减乘除运算的结果
module.exports = class comput {
constructor(a, b) {
this.a = a;
this.b = b;
}
addition() {
return this.a + this.b;
}
subtraction() {
return this.a - this.b;
}
multiplication() {
return this.a * this.b;
}
division() {
return this.a / this.b;
}
}
test.js
//引入计算模块
const Q_add = require('Q_add');
var add = new Q_add(5,5);
console.log('加法结果:'+add.addition());//加
console.log('减法结果:'+add.subtraction());//减
console.log('乘法结果:'+add.multiplication());//乘
console.log('除法结果:'+add.division());//除
在test.js所在文件夹打开cmd,运行 node test.js,得到运行结果
2、get和post区别?
区别:
- GET提交
- 1、一般是从服务器上获取数据
- 2、提交的数据会出现在地址栏上
- 3、不安全
- 4、提交的数据量小,不会超过2KB
- 5、服务器端是用Request.QueryString获取变量的值
- post提交
- 1、一般是向服务器传送数据
- 2、提交的数据不会出现在地址栏上
- 3、较安全的
- 4、提交的数据不限,(没有限制):上传都用POST提交,就不能GET提交
- 5、服务器端用Request.Form获取提交的数据
使用:
- 建议使用GET/POST方法的情况
- 1、GET方式的安全性较POST方式要差些,包含机密信息的话,建议用POST数据提交方式
- 2、在做数据查询时,建议用GET方式,而在做数据添加、修改或删除时,建议用POST方式
- 只能使用POST
- 1、无法使用缓存文件(更新服务器上的文件或数据库)
- 2、向服务器发送大量数据(POST没有数据量限制)
- 3、发送包含未知字符的用户输入时,POST比GET更稳定也更可靠
3、登录和注册功能?
文件结构:
|——demo
|——node_modules
|——www
|——formIndex
|——loginIndex.html
|——signIndex.html
|——q_test.js
loginIndex.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form action="http://localhost:8989/submit" method="get">
<!--如果是form直接提交数据,前端一定要有name值,才可以发送数据给后台-->
<!--name相当于前后端接口-->
<input type="text" placeholder="请输入用户名" name="username"><br><br>
<input type="password" placeholder="请输入密码" name="password"><br><br>
<input type="submit">
</form>
</body>
</html>
signIndex.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<h1>注册</h1>
<form action="http://localhost:8989/submit" method="post">
<!--如果是form直接提交数据,前端一定要有name值,才可以发送数据给后台 -->
<!--name相当于前后端接口-->
<input type="text" placeholder="请输入用户名" name="username"><br><br>
<input type="password" placeholder="请输入密码" name="password"><br><br>
<input type="submit">
</form>
</body>
</html>
q_test.js
//引入模块
const fs = require('fs');
const http = require('http');
const path = require('path');
const url = require('url');
const querystring = require('querystring');
//创建服务
let myServer = http.createServer((req, res) => {
if (req.url === '/favicon.ico') return;
if (req.url.startsWith('/formIndex')) {//读取静态页面
fs.readFile(path.join(__dirname, 'www', req.url), 'utf-8', (err, data) => {
if (err) throw err;
res.end(data);
});
} else if (req.url.startsWith('/submit')) {//提交信息处理
res.writeHead(200, {'Content-type':'text/plain;charset=utf-8'});//编码格式 utf-8
let params;//用于接收含有提交信息的对象
if (url.parse(req.url, true).query.username) {//get请求
console.log('GET请求,登录')
params = url.parse(req.url, true).query;
console.log('前端传的参数:' + params)
res.end(`姓名:${params.username},密码:${params.password}`);//响应内容
} else {//post请求
let allData = '';
req.on('data', (chunk) => {
allData += chunk;
});
req.on('end', () => {
console.log('POST请求,注册')
params = querystring.parse(allData);
console.log('前端传的参数:' + params)
res.end(`姓名:${params.username},密码:${params.password}`);//响应内容
});
}
} else {//找不到页面,返回404
res.end(`<h1>404 Not Found</h1>`);
}
});
//监听服务
myServer.listen('8989', (err) => {
if (err) throw err;
console.log('服务器已启动,访问端口号为:8989');
});
在q_test.js 所在文件夹打开命令行,运行node q_test.js
1)在浏览器输入:http://localhost:8989/
2)在浏览器输入:http://localhost:8989/formIndex/signIndex.html
后台获取访问参数:
响应给前端:
3)在浏览器输入:http://localhost:8989/formIndex/loginIndex.html
后台获取访问参数:
响应给前端: