学习目标:
express
mysql DML
数据建模目录
1 express
express是基于 Node.js 平台,快速、开放、极简的 Web 开发框架。
1.1 helloworld手动创建工程
$ mkdir myapp $ cd myapp $ npm init $ npm install express --save
核心代码
const express = require('express') const app = express() const port = 3000 app.use(express.static('public')) app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })
在浏览器中访问
1.2 路由定义与使用
定义了针对客户端请求的处理函数,不同uri对应不同的处理函数。通过Express的
实例对象app上的请求方法定义不同的路由,例如:get、post、delete、put、all...
在当前项目内创建routes/users.js文件内容如下
var express = require('express'); var router = express.Router(); /* GET users listing. */ router.get('/', function (req, res, next) { res.send('respond with a resource'); }); module.exports = router;
在main.js中使用users路由
const express = require('express') const usersRouter = require('./routes/users'); //注意这里 const app = express() const port = 3000 app.use(express.static('public')) app.get('/', (req, res) => { res.send('Hello World!') }) app.use('/users', usersRouter); //注意这里 app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })
重新启动项目,访问/路由和/users路由。
1.3 路由参数Route path: /users/:userId/books/:bookId Request URL: http://localhost:3000/users/34/books/8989 req.params: { "userId": "34", "bookId": "8989" }
Route path: /users Request URL: http://localhost:3000/users?id=1&name=terry req.query: { "id": "1", "name": "terry" }
1.4response方法
1.5 中间件
body-parser
cookie-parser
cors
serve-static2 脚手架
通过应用生成器工具 express-generator 可以快速创建一个应用的⻣架。
$ mkdir cms $ cd cms $ npx express-generator
3 sql-ddl
3.1 数据库定义
mysql> CREATE DATABASE poll; mysql> USE poll Database changed
3.2 表定义
create table <表名>( <列名><数据类型>[列级完整约束条件] [, <列名><数据类型>[列级完整约束条件]] ... [,<表级完整性约束条件>] );
DROP TABLE IF EXISTS `tbl_exam_paper`; CREATE TABLE `tbl_exam_paper` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `description` varchar(255) DEFAULT NULL, `title` varchar(255) DEFAULT NULL, `department_id` bigint(20) DEFAULT NULL, `user_id` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`), KEY `FK92534C8DF2C32590` (`department_id`), CONSTRAINT `FK92534C8DF2C32590` FOREIGN KEY (`department_id`) REFERENCE )
3.3 表修改
#添加列 mysql> alter table tableName add columnName dataType #删除列 mysql> alter table tableName drop columnName dataType #修改列 mysql> alter table tableName modify columnName dataType #删除数据库表 mysql> drop table tableName #清空数据库表内容 mysql> truncate table s_item
4 sql-dml
用于访问和处理数据库的标准的计算机语言。
4.1 查询语句
mysql> select [all|distinct] <目标列表达式> -> from <表或视图>[, <表或视图> -> [where <条件表达式>] -> [group by <列名1> -> [having<条件表达式>]] -> [order by <列名2>[asc|desc]]
单表查询
elect name from tbl_student where gender = 'male';
多表连接查询
select s.name,c.name from tbl_student s, tbl_student_course sc ,tbl_course c where s.id = sc.student_id and c.id = sc.course_id;
4.2 插入语句
insert into tbl_student(id,name,gender) values (null,'terry','gender')
4.3 修改语句
update tbl_student set name = '王五' where id = 1;
4.4 删除语句
delete from tbl_user where id = 1;
【express+Mysql DML、DQL+数据建模】express基于Node.js平台开发的Web开发框架
于 2022-06-19 08:07:20 首次发布