js5
js表单前后台交互
var express=require('express')//引入express
var app=express()
app.engine('html',require('express-art-template'))
//配置body-parser
app.use(express.urlencoded({extended:false}))
app.use(express.json())
app.get('/',function(req,res) {
//req是前端给后端的
//res是后端给前端的
res.render('index.html')
//渲染
})
//事件驱动,异步
app.post('/login',function(req,res) {
console.log(req.body)
if (req.body.account==123456&&req.body.password=='abc') {
res.render('user.html')
}else{
res.render('guester.html')
}
})
app.listen(3000,function(){
console.log('app is running at port 3000...')
})//设置端口号
注:
express.json解析JSON格式的请求体数据(有兼容性,仅在4.16.0+ 版本中可用)
express.urlencoded解析URL-encoded格式的请求体数据(有兼容性,仅在4.16.0+版本中可用)
js数组
1. 数组的声明和赋值
new Array()构造函数方法
1. 使用构造函数创建数组对象
创建了一个空数组
var arr = new Array();
创建了一个数组,里面存放了3个字符串
var arr = new Array('zs', 'ls', 'ww')
创建了一个数组,里面存放了4个数字
var arr = new Array(1, 2, 3, 4);
字面量方式
2. 使用字面量创建数组对象
var arr = [1, 2, 3];
获取数组中元素的个数
console.log(arr.length);
数组的常用方法
push() //在数组末尾添加一个或多个元素,并返回数组操作后的长度
pop() //删除数组最后一项,返回删除项
shift() //删除数组第一项,返回删除项
unshift() //在数组开头添加一个或多个元素,并返回数组的新长度
splice(a,b,c)
//在任何位置增加,删除,替换
//第一个是从哪个位置开始删
//第二个是删几个数
//第三个是添加的数
js生成随机数基本量
| ceil(x) | 对数进行上舍入,即向上取整。 |
| floor(x) | 对 x 进行下舍入,即向下取整。 |
| round(x) | 四舍五入。 |
| random() | 返回 0 ~ 1 之间的随机数,包含 0 不包含 1。 |
//生成随机数
console.log(Math.random())
//生成一个50~100之间的随机数
console.log(Math.random()*50+50)
//random取随机数
console.log(Math.round(5.6))
//四舍五入取整数
console.log(Math.ceil(5.6))
//大于该数的最小整数
console.log(Math.floor(5.6))
//小于该数的最大整数
浙公网安备 33010602011771号