GundamFans V1.2
更新:
Comment模块设计: Data Association in Mongoose, 为了安全性使用expressSanitizer
Code Refactoring: 划分module, RESTful routing, seedDB()函数生成初试测试数据
RESTful routes
RESTful routes 是一种约定俗成的router定义方式, 对应CURD(Create, Update, Read, Delete)的需求, 设计方式如下:
HTTP VERB | ROUTE | Action | Used For |
---|---|---|---|
GET | ‘/wiki’ | index action | index page to display all gundam info |
GET | ‘/wiki/new’ | new action | displays create gundam info form |
POST | ‘/wiki’ | create action | creates one gundam |
GET | ‘/wiki/:id’ | show action | displays one gundambased on ID in the url |
GET | ‘/wiki/:id/edit’ | edit action | displays edit form based on ID in the url |
PUT | ‘/wiki/:id’ | update action | replaces an existing article based on ID in the url |
DELETE | ‘/wiki/:id’ | delete action | deletes one article based on ID in the url |
PATCH | ‘/wiki/:id’ | update action | modifies an existing article based on ID in the url |
Comment Module
模式设计
1 | let gundamMSSchema = new mongoose.Schema({ |
读入数据时, 使用populate方法将 id 替换为实际数据:
1 | gundamMS.findById(req.params.id) //搜索结果为json的集合 |
安全性expressSanitizer
为了使评论格式多样, 在show页面的ejs中, 使用<%- %>
进行显示输出, 为了避免用户输入<script></script>
导致执行恶意代码, 使用expressSanitized:
1 | npm i express-sanitized |
data association in mongoose知识补充
模式声明时, 属性可以包含其他模式.
1 | //模式定义 |
ObjectId
:表示数据库中某一模型的特定实例。例如,一本书可能会使用它来表示其作者对象。它实际只包含指定对象的唯一 ID(_id
) 。可以使用 populate()
方法在需要时提取相关信息。
Code Refactoring
将schema分到单独的文件, 使用module.exports =..
来设置出口变量.
同时设计seedDB()函数为数据库增加原始数据. 注意 :为了保证操作的运行次序, 必须把存在先后次序的操作放在call back中.
1 | function seedDB(){ |