- 1. Express扩展
- 1.1 Express 中使用 art-template
- 使用步骤:
- # express-art-template中间件,首先要安装art-template,再安装express-art-template
- npm i art-template express-art-template
- 在服务器文件中加载 express-art-template const template = require('express-art-template');
- //设置模板引擎类型
- //参数1: 模板文件的后缀,此处可以随意定义。 定义成什么,模板文件后缀就是什么。一般使用html即可
- app.engine('html', template);
- 模板文件的数据渲染方式和之前art-template方式一样
- 1.2 get方式提交数据
- 使用 query 接收get数据
- req.query中以对象形式保存了get参数: {id:10001, name:"zs"}
- console.log(req.query.id);
- console.log(req.query.name);
- 使用 params 接收get数据
- req.params中以对象形式保存了get参数 req.params = {id:10, name:"ls"}
- app.get('/index/:id/:name', (req, res) => {
- console.log(req.params.id);
- console.log(req.params.name);
- // req.params = {id:10, name:"ls"}
- http://127.0.0.1:3000/index/10/ls 和 /index/:id/:name
- 1.3 post方式提交数据
- express 中接收 post方式提交的数据需要使用 body-parser 模块
- body-parser 是一个第三方模块,现在已经被 express 集成
- 使用 body-parser 来接收表单数据时,表单数据会直接挂载到 req对象的 body属性下,以对象形式保存
- 使用步骤:
- 1)加载 body-parser 模块 const bodyParser = require('body-parser');
- 2)注册为中间件
- app.use(bodyParser.urlencoded({extended:false}))
- // {extended:false} 代表使用node的 querystring 模块来解析表单数据
- 3)使用req对象的body属性来获取表单数据console.log(req.body)
- 2. MySQL数据库
- 2.1 什么是数据库
- 存储数据的仓库。常见的数据库: MySQL、 Oracle、 Sqlserver 等。
- 2.2 MySQL简介
- MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品
- MySQL结构:

- 2.3 安装客户端
- 常见的客户端: CMD 、 Navicat、 Sqlyog、 phpmyadmin等等
- 3.数据查询
- 要操作MySQL数据库需要使用到SQL语句。 SQL语句全称是结构化查询语言,使用该语言能够快速的从数据表中读取我们需要的数据,或者添加、删除、修改某条或某些数据。
- 语法格式:
- SELECT 字段名1, 字段名2, ......
- [ ORDER BY <字段名> [ ASC|DESC ]]
- 基本查询
- 格式: select 字段名1, 字段名2,.... from 表名
- 例select sno,sname from student
- 使用 * 代替字段名,系统会将 * 解析为所有的字段名select * from student
- 带where子句的查询
- select field1, field2... from 表名 查询表中的所有数据
- 模糊查询
- 通配符:
- like: 在执行模糊查询时,必须使用like来作为匹配条件
- 查询结果排序
- order by 可以对查询结果按某个字段进行升序或者降序排列
- 可进行排序的字段通常是 整型 英文字符串型 日期型 (中文字符串也行,但一般不用)
- 限制查询结果
- var1: 起始点。 查询结果的索引,从0开始。 0代表第一条数据
|