5 基于Koa的egg.js企业级后端框架使用

代码探云鹤
• 阅读 3561

课堂目标

  1. 熟悉企业级web开发框架egg.js使用
  2. 基于Koa定制自己的企业级MVC框架

Egg.js体验

  • 架构图

5 基于Koa的egg.js企业级后端框架使用

  • 创建项目

    // 创建项目
    
    $ npm i egg-init \-g
    
    $ egg\-init egg-example \--type\=simple
    
    $ cd egg-example
    
    $ npm i
    
    // 启动项目
    
    $ npm run dev
    
    $ open localhost:7001
  • 浏览项目结构:

    • Public
    • Router -> Controller -> Service -> Model
    • Schedule
  • 创建一个路由,router.js

    router.get('/user', controller.user.index);
  • 创建一个控制器,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;
  • 创建一个服务,./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;
  • 使用服务,./app/controller/user.js
async index() {
  const { ctx } = this;
  ctx.body = await ctx.service.user.getAll();
}
  • 创建模型层:以mysql + sequelize为例演示数据持久化

    • 安装: 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;
    };
点赞
收藏
评论区
推荐文章
zdd小小菜鸟 zdd小小菜鸟
3年前
spring面试
spring面试1.什么是spring?TEXSpring是个java企业级应用的开源开发框架。Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用。Spring框架目标是简化Java企
Jacquelyn38 Jacquelyn38
4年前
Nodejs实现图片的上传、压缩预览、定时删除
前言我们程序员日常都会用到图片压缩,面对这么常用的功能,肯定要尝试实现一番。第一步,node基本配置这里我们用到的是koa框架,它可是继express框架之后又一个更富有表现力、更健壮的web框架。1、引入基本配置constKoarequire('koa');//koa框架constRouterrequire('koaroute
Stella981 Stella981
4年前
Polaris【北极星】企业级云原生微服务框架
!(https://oscimg.oschina.net/oscnet/f4418fe6cc8d4fb2a9770638a0dee1db.png)Polaris【北极星】企业级云原生微服务框架①Polaris【北极星】企业级云原生微服务框架文档Polaris【北极星】企业级云原生微服务框架(PDF版)Pol
Wesley13 Wesley13
4年前
Spring MVC入门
2.1、SpringWebMVC是什么SpringWebMVC是一种基于Java的实现了WebMVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求响应模型,框架的目的就是帮助我们简化开发,SpringWebMVC也是要简化我们日常Web开发的。另外还有一种基于
Stella981 Stella981
4年前
Ant Design 3.0.3 发布,阿里企业级 UI 设计语言
!(https://oscimg.oschina.net/oscnet/e660b58d2f9e456cb8a8b72150af9c62.png)AntDesign是蚂蚁金服开发和正在使用的一套企业级的前端设计语言和基于React的前端框架实现。!(https://oscimg.oschina.net/oscnet/490
Stella981 Stella981
4年前
Serverless + Egg.js 后台管理系统实战
作为一名前端开发者,在选择Nodejs后端服务框架时,第一时间会想到Egg.js(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Feggjs%2Fegg%2F),不得不说Egg.js是一个非常优秀的企业级框架,它的高扩展性和丰富的插件,极大的提高了
黄忠 黄忠
3年前
无mi基于 Flutter 3.x 仿抖音跨平台混合开发细柳新蒲为谁绿
下栽ke呈:基于Flutter3.x仿抖音跨平台混合开发(https://www.sisuoit.com/3037.html)优点和缺点优势:Spring产生于Spring家族,在企业级开发框架中无人能及,地位举足轻重,可以保