egg.js是什么

是一个node.js的后台web框架,类似的还有express,koa

优势:规范插件机制
Egg.js约定了一套代码目录结构
(配置config、路由router、扩展extend、中间件middleware、控制器controller)
规范的目录结构,可以使得不同团队的开发者使用框架写出来的代码风格会更一致,接手成本也会更低。

使用场景

BFF层(前后端之间的中间层)、全栈、SSR(服务端渲染)

目录结构

框架约定的目录


app/router.js 用于配置URL路由规则
app/contorller/** 用于解析用户的输入,处理后返回相应的结果
app/service/** 用于编写业务逻辑层 【可选】
app/middleware/** 用于编写中间件 【可选】
app/service/** 用于框架的扩展 【可选】
...
自定义的目录
app/view/** 用于防止模板文件 【可选】

加载顺序

Egg 将应用、框架和插件都称为加载单元(loadUnit);

在加载过程中,Egg会遍历所有的加载单元,加载时有一点的优先级
· 插件 => 框架 => 应用依次加载
· 依赖方先加载
· 框架按继承顺序加载,越底层越先加载

如有这样一个应用配置了如下依赖


app
| ├── plugin2 (依赖 plugin3)
| └── plugin3
└── framework1
| └── plugin1
└── egg

最终的加载顺序为


=> plugin1
=> plugin3
=> plugin2
=> egg
=> framework1
=> app

文件加载顺序


package.json => config => app/extend => app.js => app/service => app/middleware => app/controller => app/router.js

router

首先我们需要配置路由
因为我们在实际的开发中会使用很多路由所以这里我们将路由改成分级的
在app下创建一个router文件夹用来存放路由文件home.js


//app/router/home.js
'use strict' module exports = app => {
const { home } = app.controller //获取controller下的home.js
app.get('/',home.index);
app.get('/home/param',home.getParam);
app.get('/home/postParam',home.postParam);
} // app/router.js
'use strict'
const RouteHome = require('./router/home');
module.exports = {
const {router, controller} = app;
RouteHome(app);
} //app/controller/home.js 'use strict' const Controller = require('egg').Controller; class HomeController extends Controller {
async index() {
await this.ctx.render('/index',{name:'egg'});
}
async getParam() {
let id = await this.ctx.query.id;
this.ctx.body = id;
}
async postParam(){
let id = await this.ctx.request.body.id; //获取post参数
this.ctx.body = id;
}
}
module exports = HomeController;

controller

我们通过 Router 将用户的请求基于 method 和 URL 分发到了对应的 Controller 上, Controller 负责解析用户的输入,处理后返回相应的结果
框架推荐 Controller 层主要对用户的请求参数进行处理(校验、转换),然后调用对应的 service 方法处理业务,得到业务结果后封装并返回

  • 所有的 Controller 文件都必须放在 app/controller 目录下,可以支持多级目录
  • 访问的时候可以通过目录名级联访问

//在app/controller目录下 新建一个controller
'use strict' const Controller = required('egg').Controller; class CustomerController extends Controller {
async customIndex() {
//ctx.body 是 ctx.response.body 的简写,不要和 ctx.request.body 混淆了
this.ctx.body = 'this is my controller';
}
}
module.exports = CustomController; //在router.js中配置路由(访问时请求的路径) 'use strict' module.exports = app => {
//相当于拿到app文件夹下的router和controller
const {router, controller} = app;
router.get('/', controller.home.index);
router.get('/custom',controller.customerController.customIndex);
}

定义的 Controller 类,会在每一个请求访问到 server 时实例化一个全新的对象,而项目中的 Controller 类继承于 egg.Controller,会有下面几个属性挂在 this 上


- this.ctx 框架封装好的处理当前请求的各种便捷属性和方法
- this.app 框架提供的全局对象和方法
- this.service 访问抽象出的业务层 相当于 this.ctx.service
- this.config 运行的配置项
- this.logger 日志

service

业务逻辑层



'use strict';
const Service = require('egg').Service;
class customService extends Service {
async show(zc, hh) { //异步防阻塞
return zc + " and " + hh;
}
}
module.exports = UserService; //controller代码
'use strict'; const Controller = require('egg').Controller; class CustomController extends Controller {
async custonIndex() {
let str = await this.ctx.service.customService.show('zc','hh');
//这里使用await来获取异步方法的返回值
this.ctx.body = 'this is my controller'+str;
}
} module.exports = CustomController;

一个更完整的栗子


// app/router.js
module.exports = app => {
app.router.get('/user/:id', app.controller.user.info);
}; // app/controller/user.js
const Controller = require('egg').Controller;
class UserController extends Controller {
async info() {
const userId = ctx.params.id;
const userInfo = await ctx.service.user.find(userId);
ctx.body = userInfo;
}
}
module.exports = UserController; // app/service/user.js
const Service = require('egg').Service;
class UserService extends Service {
// 默认不需要提供构造函数。
// constructor(ctx) {
// super(ctx); 如果需要在构造函数做一些处理,一定要有这句话,才能保证后面 `this.ctx`的使用。
// // 就可以直接通过 this.ctx 获取 ctx 了
// // 还可以直接通过 this.app 获取 app 了
// }
async find(uid) {
// 假如 我们拿到用户 id 从数据库获取用户详细信息
const user = await this.ctx.db.query('select * from user where uid = ?', uid); // 假定这里还有一些复杂的计算,然后返回需要的信息。
const picture = await this.getPicture(uid); return {
name: user.user_name,
age: user.age,
picture,
};
} async getPicture(uid) {
const result = await this.ctx.curl(`http://photoserver/uid=${uid}`, { dataType: 'json' });
return result.data;
}
}
module.exports = UserService; // curl http://127.0.0.1:7001/user/1234

helper

app/extend文件夹里面存放工具类



   //app/extend/getName.js
'use strict'
module.exports = {
getUserName(id) {
return list.find(i=>i.id===id).name;
}
} //app/extend/helper.js
'use strict' const getName = require('./getName');
module.exports = {
showName() {
return getName.getUserName('2221');
}
} //controller引用helper
'use strict' const Controller = require('egg').Controller;
class CustomController extends Controller {
async customIndex() {
////this.ctx.helper拿到helper内置对象也就是进入helper.js这个文件
this.ctx.body = this.ctx.helper.showName();
}
}
module.exports = CustomController;

页面渲染

egg.js使用的是nunjucks页面模板


//在config/plugin.js里面添加 'use strict' exports.nunjucks = {
enable: true,
package: 'egg-view-nunjucks'
} //config/config/default.js 添加 'use strict'
...
module.exports = app => {
...
config.view = {
mapping: {
'.html': 'nunjucks'
},
root: path.join(appInfo.baseDir, 'app/view')
}
...
return config;
} //app/routes/sign.js 'use strict'; module.exports = (router, controller) => {
router.get('/sign/modifyPassword', controller.sign.modifyPassword);
}; //app/controller/sign.js 'use strict'; const Controller = require('egg').Controller; class SignController extends Controller {
async modifyPassword() {
const { ctx } = this;
//渲染view/sign/modifyPassword.html
await ctx.render('sign/modifyPassword.html');
}
} module.exports = SignController;

来源:https://segmentfault.com/a/1190000016378796

Egg.js学习的更多相关文章

  1. Egg.js学习与实战系列 · Post请求`csrf token`问题

    在使用axios请求egg.js封装的post接口时出现missing csrf token 或 invalid csrf token.踩过坑的新手估计不在少数,本篇记录一下解决方法. 问题原因 引用 ...

  2. Egg.js学习与实战系列 · 文件上传配置

    在使用Egg.js搭建文件上传服务时,遇到了几个一般新手都会遇到的坑. 经查阅官方文档,Egg框架中默认使用egg-multipart插件进行文件上传,所以上传文件前需要做相关的配置. 上传文件提示: ...

  3. egg.js 学习之 中间件使用

    1.在框架和插件中使用中间件 编写中间件 我们先来通过编写一个简单的中间件,来看看中间件的写法. // app/middleware/middlewareOne.js // app/middlewar ...

  4. Egg入门学习(三)---理解中间件作用

    Egg是基于koa的,因此Egg的中间件和Koa的中间件是类似的.都是基于洋葱圈模型的. 在Egg中,比如我想禁用某些IP地址来访问我们的网页的时候,在egg.js中我们可以使用中间件来实现这个功能, ...

  5. Egg入门学习(二)---理解service作用

    在上一篇文章 Egg入门学习一 中,我们简单的了解了Egg是什么东西,且能做什么,这篇文章我们首先来看看官网对Egg的整个框架的约定如下,及约定对应的目录是做什么的,来有个简单的理解,注意:我也是按照 ...

  6. Egg入门学习(一)

    一:什么是Egg? 它能做什么?Egg.js是nodejs的一个框架,它是基于koa框架的基础之上的上层框架,它继承了koa的,它可以帮助开发人员提高开发效率和维护成本.Egg约定了一些规则,在开发中 ...

  7. Node.js框架之Egg.js

    Node.js是我前段时间接触的一个JavaScript的服务端语言,感觉还是挺有意思的. 也许有人说,你学这么多,学的过来吗?或者说学的太多,专而不精,有必要这样吗? 其实,我个人认为,自从我进入I ...

  8. egg.js源码解析之render()

    作为阿里的开源node.js框架,我觉得egg.js是很有前途的,故而学之,github上down了一个项目下来(https://github.com/easy-team/egg-vue-webpac ...

  9. egg.js搭建 api设置跨域

    1.egg简述 Egg.js,为企业级框架和应用而生,是阿里开源的企业级 Node.js 框架. 2.特点 Egg 奉行『约定优于配置』,按照一套统一的约定进行应用开发,团队内部采用这种方式可以减少开 ...

随机推荐

  1. C++奇淫技巧

    一.关于:的妙用 如下代码 #include<cstdio> #include<iostream> typedef struct point{ int a; int b; po ...

  2. Spring Cloud--尚硅谷2020最新版

    Spring Cloud 初识Spring Cloud与微服务 在传统的软件架构中,我们通常采用的是单体应用来构建一个系统,一个单体应用糅合了各种业务模块.起初在业务规模不是很大的情况下,对于单体应用 ...

  3. Java基础的基础,花1h看这一篇就够了!

    ------------恢复内容开始------------ Java笔记 ​ 一直以来,总想着Java会点基础就可以写后端程序了,但越到后来越发现Java基础的重要性.更不必说在面试时,Java基础 ...

  4. 《HelloGitHub》第 53 期

    兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...

  5. laravel+vue+vuetify 前端匹配不到数据记录 No matching records found

    后端数据:使用guzzle获取api数据,(安装扩展包guzzle) use GuzzleHttp\Client; //获取请求远程产品信息需要的参数public function getParams ...

  6. Ingress-nginx 与 Nginx-ingress

    一.概述 Ingress-nginx:它是由Kubernetes社区基于Nginx Web服务器开发的,并补充了一组用于实现额外功能的Lua插件,作为“官方”默认控制器支持当然最优. Github:h ...

  7. oeasy教你玩转linux010104灵魂之问whatis

    灵魂之问whatis 回忆上节课 我们上次在系统里面乱转

  8. mac 下配置连接Linux服务器方法,上传下载文件操作

    1.先按照文档在本地生成SSHkey 2.mac输入 sudo -i 进入超级管理员#模式下,然后 创建用户 #useradd XXXadmin #passwd XXXadmin XXXadmin用户 ...

  9. Volatile关键字&&DCL单例模式,volatile 和 synchronized 的区别

    Volatile 英文翻译:易变的.可变的.不稳定的. 一.volatile 定义及用法 多个线程的工作内存彼此独立,互不可见,线程启动的时候,虚拟机为每个内存分配一块工作内存,不仅包含了线程内部定义 ...

  10. Sqli-labs 1-10

    Less 1-4(基础注入) 基础知识: table_schema:数据库的名称 table_name:表的名称 column_name:列的名称 information_schema:表示所有信息, ...