第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【金秋打卡】第5天 Node.js+Koa2+MySQL打造前后端分離精品項(xiàng)目《舊島》

標(biāo)簽:
Node.js JavaScript

课程章节: 【构建用户身份系统】通用用户系统与小程序用户系统

课程讲师: 7七月

课程内容:

Sequelize个性化配置与数据维护策略

在 db.js 中

sequelize.sync() // 如果不加的话, sequelize库是不会把这些模型全部创建到数据库里

sequelize.sync({force: true}) 为 true 话 新增了参数会删除原有的表,新增有参数的表   上正式千万不要设置成true

// 如果不要update_time, create_time, delete_time 需要在   非常有用不建议删除
define: {
	timestamps: false,  // 还是需要改成true
	paranoid: true, // 会生成一个 delete_time 的时间戳
	createdAt: 'created_at',
	updateAt: 'updated_at',
	deletedAt: 'deleted_at',
	underscored: true, // 通过这个可以把驼峰转换成下划线
}

在 models/user.js中
User.init 参数接收两个对象, 最后一个对象接收

tableName: 'user', // 指定表名

LinValidator综合应用

在 创建 api/v1/user.js 编写api
用户注册

const Router = require('koa-router')
const router = new Router({prefix: '/v1/user'})
const { RegisterValidator } = require(validator)
// 注册 新增数据 put get delete
router.get('/register', async (ctx) => {
	// 接收参数
	// email password1 password2 phone
	const v = new RegisterValidator().validate(ctx) // 传入
})
module.exports = router

在 validator.js
注册 效验

class RegisterValidator extends LinValidator {
	constructor() {
		super()
		this.email = [
			new Rule('isEmail', '不符合Email规范')
        ]
        this.password1 = [
			new Rule('isLength', '密码至少6个字符, 最多32个字符', { min: 6, max: 32 }),
			new Rule('matches', '密码不符合规范', '^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]')
        ]
        this.password2 = this.password1 // 效验规则相同
        this.nickname = [
            new Rule('isLength', '昵称不符合长度规范', {
            min: 6,
            max: 32
            })
    ]
    }
    validatePassword(vals) {
    const psw1 = vals.body.password1
    const psw2 = vals.body.password2
    if (psw1 !== psw2) {
      throw new Error('两个密码必须相同')
    }
  }
}

导出这个类
module.exports = {
	RegisterValidator
}

出现报错
middlewares/exception.js

    // 开发环境
    const isHttpException = error instanceof HttpException
    const isDev = global.config.environment === 'dev'
    // 如果是开发环境 不是HttpException这个类处理的 就直接报错
    if (isDev && !isHttpException) {
      throw error
    }
        // 生产环境
    // 已知错误
    if (isHttpException) {
      ctx.body = {
        msg: error.msg,
        errorCode: error.errorCode,
        request: `${ctx.method} ${ctx.path}`
      }
      ctx.status = error.code
    } else {
      // 未知异常
      ctx.body = {
        msg: 'we made a mistake',
        errorCode: 999,
        request: `${ctx.method} ${ctx.path}`
      }
      ctx.status = 500
    }

课程收获

对 sequelize 的参数有一定了解

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消