免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項超值服

開通VIP
5 基于Koa的egg.js企業(yè)級后端框架使用

課堂目標(biāo)

  1. 熟悉企業(yè)級web開發(fā)框架egg.js使用
  2. 基于Koa定制自己的企業(yè)級MVC框架

Egg.js體驗

  • 架構(gòu)圖

  • 創(chuàng)建項目

    // 創(chuàng)建項目$ npm i egg-init \-g$ egg\-init egg-example \--type\=simple$ cd egg-example$ npm i// 啟動項目$ npm run dev$ open localhost:7001
  • 瀏覽項目結(jié)構(gòu):

    • Public
    • Router -> Controller -> Service -> Model
    • Schedule
  • 創(chuàng)建一個路由,router.js

    router.get('/user', controller.user.index);
  • 創(chuàng)建一個控制器,user.js

    'use strict';  const Controller = require('egg').Controller;  class UserController extends Controller {      async index() {          this.ctx.body = [{name: 'tom'}, {name: 'jerry'}\]      }  }  module.exports = UserController;
  • 創(chuàng)建一個服務(wù),./app/service/user.js

    'use strict';  const Service \= require('egg').Service;  class UserService extends Service {      async getAll() {          return \[              {name: 'tom'},              {name: 'jerry'}          \]      }  }  module.exports \= UserService;
  • 使用服務(wù),./app/controller/user.js
async index() {??const { ctx } = this;??ctx.body = await ctx.service.user.getAll();}
  • 創(chuàng)建模型層:以mysql + sequelize為例演示數(shù)據(jù)持久化

    • 安裝: npm install --save egg-sequelize mysql2
    • 在 config/plugin.js 中引入 egg-sequelize 插件

      sequelize: {  enable: true,  package: 'egg-sequelize',}
    • 在 config/config.default.js 中編寫 sequelize 配置

      sequelize: {   dialect: "mysql",   host: "127.0.0.1",   port: 3306,   username: "root",   password: "admin",   database: "test"}
    • 編寫User模型,./app/model/user.js
    module.exports = app => { const { STRING } = app.Sequelize; const User = app.model.define(  "user", { name: STRING(30) }, { timestamps: false }); return User;};
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
在Sequelize中使用遷移
阿里開源框架egg.js入門與實戰(zhàn)
【全文】狼叔:如何正確的學(xué)習(xí)Node.js
4 Koa實戰(zhàn) - Restful API
搭建一個低配版的Mock Server
Node.js框架比較: Express vs. Koa vs. Hapi
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服