初始化express
express npm i 1
创建model文件夹,分别创建db文件和model文件
db文件内容
const mongoose = require( "mongoose") / /连接数据库 mongoose.connect( "mongodb://127.0.0.1:27017/db2209") / /创建一个连接对象 const conn = mongoose.connection / /利用 on方法监听事件 open 表示连接成功 error 表示连接失败 conn. on( "open",() = >{ console.log( "数据库连接成功"); }) conn. on( "error",(err) = >{ console.log( "数据库连接失败",err); }) module.exports = mongoose
model文件内容
/ ** * Schema 对象 定义集合的模式结构 命名规范 : xxxSchema * Model 对象 根据该对象映射成一个集合 命名规范 : xxxModel * Document 文档对象 定义文档 * 上面三个对象定义时注意顺序,先定义Schema 再定义Model 最后添加Document */ / / 1-连接数据库 const mongoose = require( "./1-利用mongoose连接数据库") / / 2-定义Schema对象 定义文档的模式结构 const userSchema = new mongoose.Schema({ username : String, / /mongoose中常用的数据类型 : ObjectID String Number Boolean Date Array password : String, birth : Date, sex : Boolean }) / / 3-定义Model对象 model( 实际的model名称 , 该model对应的Schema对象 , 实际的集合名称 ) / /注意 : 第三个参数可以省略的,如果省略了,实际的集合名称就是 参数 1 的复数形式 / / 如果不省略第三个参数,建义 实际的集合名称和实际的model名称一样 const userModel = mongoose.model( "user" , userSchema , "user")
配置跨域
// 下载cors,在app.js文件配置跨域、图片 var cors = require( 'cors') app. use( cors()) app. use( "/upload",express. static( "upload"))
展示、增删改
var express = require( 'express'); var router = express.Router(); var mul = require( 'multiparty') var {userModel,bookModel} = require( '../model/model') / * GET home page. * / / / 图片 router.post( '/pic', function(req, res, next) { let form = new mul.Form() form.uploadDir = "upload" form.parse(req,(ree,formdata,imgdata) = >{ res. send({ code: 200, pic: "http://localhost:3000/" +imgdata. file[ 0].path }) }) }); / / 展示 router.post( '/login',async function(req, res, next) { let data = req.body console.log( data); let loginn = await userModel.find({name: data.name}) console.log(loginn); if( data.name = = "admin"){ if( data.pw = = = "123456"){ res. send({ code: 200, msg: "登陆成功" }) } else{ res. send({ code: 201, msg: "密码错误" }) } } else{ res. send({ code : 202, msg: "用户不存在" }) } }); router. get( '/list',async function(req, res, next) { let key = req.query. key let cp = req.query.cp let ps = req.query.ps let datashow = await bookModel.find({ status: false}) let hsz = await bookModel.find({ status: true}) res. send({ code: 200, hsz, datashow, }); }); / / 添加 router.post( '/add',async function(req, res, next) { let body = req.body await bookModel.create(body) res. send({ code: 200, msg: "添加成功" }); }); / / 修改 router.post( '/up',async function(req, res, next) { let body = req.body await bookModel.updateOne({_id:body._id},body) res. send({ code: 200, msg: "修改成功" }); }); router.post( '/uphsz',async function(req, res, next) { let body = req.body / / console.log(body); await bookModel.updateOne({_id:body._id},body) res. send({ code: 200, msg: "修改成功" }); }); / / 删除和删除选中 router. get( '/del',async function(req, res, next) { let _id = req.query.id await bookModel.deleteOne({_id}) res. send({ code: 200, msg: "删除成功" }); }); router.post( '/delc', function(req, res, next) { let body = req.body console.log(body); body.forEach(async (item) = > { await bookModel.deleteOne({_id:item._id}) }) res. send({ code: 200, msg: "删除选中成功" }); }); module.exports = router;