server
1. webServer
2. apiServer( 后端工程师, 前端写接口 ) ---》 测试工具(postman insomina )
1. express
1. express中一个路由就是一个接口
2. koa
架构思想
需求: 将express框架 ---》 RMVC框架
RMVC(把route.js中的接口分离,只留下路由中间件)
R ---》 Router 路由
M ---> Model 数据
V ---> View 视图
C ---> Controller 控制器(逻辑)
需求: 再打造一个接口 http://localhost:3000/login
mongodb(命令) --> 连接数据库、操作数据库 ----》
mongoose模块
使用步骤:
1. 安装mogonose
`npm i mongoose -S`
2. 导入
`const mongoose = require('mongoose')`
3. 连接数据库
`mongoose.connect('mongodb://主机名:端口/数据库名称')`
举例: `mongoose.connect('mongodb://127.0.0.1:27017/1901')`
4. 存储数据步骤:定义Schema (骨架) > 创建model(模型)> Entity实例化方法。
新增数据
Schema:一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力
Model:由Schema发布生成的模型,具有抽象属性和行为的数据库操作对
Entity:由Model创建的实体,他的操作也会影响数据库
存储数据步骤:定义Schema (骨架) > 创建model(模型)> Entity实例化方法。
Schema
Schema数据类型: string ,number, date, buffer , boolean,mixed,objectId,Array
Schema的创建:
var Schema = mongoose.Schema;
var studentSchema = new Schema({
name: String,
age: String});
Model
model通过schema创建
var studentModel = mongoose.model("students",studentSchema );
entity
var instance1 = new studentModel ();
instance1.name="tangyan";
instance1.age="18";
数据库操作
//添加 save/insert
instance1.save(function(err){
if (err) { console.log('保存失败'); return; } });
//删除 remove(得先找到id,根据id进行删除操作)
var id=”57e24521a755e1154039a403”;
studentQueryModel.findById(id,function(err,doc) { if(!doc){
return next(new NotFound("Doc not found"))
}else{
doc.remove(function(){
console.log('删除成功');
})
}
//查询 find
studentQueryModel.find({},function(err,docs)
{ console.log(docs);
});
//修改 find+save
var id=”57e24521a755e1154039a403”;
studentQueryModel.findById(id,(err,doc)=>
{ //console.log(doc);
doc.name="xiaotangyan" ;
doc.save(function(err){
if(!err){
console.log('修改成功');
}else{
throw err;
} });});
gulp搭建项目(跨域) MongoDB的可视化工具 Robo3T: https://robomongo.org/download adminMongo: https://blog.csdn.net/yzy199391/article/details/80453349