(一) pm2.config.js
const app = {
name: "interview-server",
script: "./app.js",
watch: true,
env: {
PORT: 3012, // 端口号
publicPath: "localhost:3012", // 暂时用不着
NODE_ENV: "development", // 环境
DB_ADDR: "localhost/interview" // localhost ip地址,interview是数据库的名称
},
env_production: {
PORT: 3012,
publicPath: "132.232.94.151:3012",
NODE_ENV: "production",
DB_ADDR: "xxx.xxx.xxx.xxx/interview" //xxx.xxx.xxx.xxx你的服务器ip地址
}
};
module.exports = {
apps: app
};
(二) app.js 项目入口文件
(三) plugins 放置中间件
(四) public 静态资源目录
(五) src 源码,就是我们编写的逻辑代码
(六) router 路由配置
(七) src的模块说明,以film模块进行举例
-
atts.js 设置这个模块的字段
filmName: { type: String, // 类型 required: true, // 是否必须 default: "电影名称" // 默认值 },
// 完整 let moment = require("moment"); const uuidv1 = require("uuid/v1")(); const id = uuidv1.replace(/-/g, ""); module.exports = { filmName: { type: String, required: true, default: "电影名称" }, filmId: { type: String, default: id }, poster: String, grade: Number, actors: Array, runtime: Number, createTime: { type: String, default: moment().format("YYYY-MM-DD HH:mm:ss") } };
-
model mvc中的m(数据模型,负责跟数据库进行交互)
// attrs是film集合的字段 const attrs = require("./attrs"); const mongoose = require("mongoose"); class Model { constructor() { // schema让我们设置attrs里的规则生效 let schema = mongoose.Schema(attrs); // mongoose.model 创建一个model,用来跟数据库进行交互 return mongoose.model("film", schema); } } module.exports = new Model();
-
route film模块的路由配置
-
service 接口的业务逻辑,下面对电影模块的增删改查进行解释
// (1)继承公共类 class Service extends Common { constructor() { // 调用父类构造器,完成继承动作 super("film"); this.model = this.getModel("film"); } }
// 添加书籍 async add(ctx) { try { let data = { ...ctx.params, ...this.defaultData }; let model = this.model(data); // 创建model let res = await model.save(); // 保存数据 return this.handleSuccess(res); } catch (error) { return this.handleError(error); } }
// 查找全部电影 async list(ctx) { try { let res = await this.model.find(); return this.handleSuccess(res); } catch (error) { return this.handleError(error); } }
// 查找电影详情 async detail(ctx) { try { let { filmId } = ctx.params; let res = await this.model.findOne({ filmId }); return this.handleSuccess(res); } catch (error) { return this.handleError(error); } }
// 删除 async del(ctx) { try { let { filmId } = ctx.params; let res; if (typeof filmId === 'string') { res = await this.model.deleteOne({ filmId }); } else { res = await this.model.deleteMany({ filmId }); } return this.handleDelete(res); } catch (error) { return this.handleError(error); } }
// 修改 async edit(ctx) { try { let { filmId } = ctx.params; let res = await this.model.updateOne({ filmId }, ctx.params); return this.handleSuccess(res); } catch (error) { return this.handleError(error); } }
// 购物车的添加
add(data) {
let promise = new Promise(async (resolve, reject) => {
try {
// 添加价格
data.price = data.minPrice;
// 合并同类商品的购物车
// 根据userId和productId查找cart
let query = { userId: data.userId, productId: data.productId };
let cart = await this.find(query);
cart = cart[0];
// 没有就新增,否则在原有购物车上修改
if (!cart) {
let model = this.model(data);
let res = await model.save();
resolve(this.handleData(res));
} else {
let cartId = cart.cartId;
let buyNum = Number(cart.buyNum) + Number(data.buyNum);
let res = await this.updateNum({ cartId, buyNum });
res = {
...res,
...cart
};
resolve(res);
}
} catch (error) {
reject(error);
}
});
return promise;
}
/**
* 可以根据id或者其他条件查询
* @param {*} query
*/
find(query) {
return new Promise(async (resolve, reject) => {
try {
let { cartId } = query;
let res;
if (cartId) {
res = await this.model.find({ _id: cartId });
} else {
res = await this.model.find(query);
}
resolve(this.handleData(res));
} catch (error) {
reject(error);
}
});
}
网友评论