egg 的学习

1.初始化项目

快速生成项目:

$ npm i egg-init -g
$ egg-init egg-example --type=simple
$ cd egg-example
$ npm i

  

启动项目:

$ npm run dev
$ open localhost:7001

2.安装启动数据库

npm i --save egg-mysql

 开启插件:

开启插件:
exports.mysql = {
enable: true,
package: 'egg-mysql',
};

 3.配置数据库

  config.mysql = {
    // 单数据库信息配置
    client: {
      // host
      host: 'api',
      // 端口号
      port: '3306',
      // 用户名
      user: 'root',
      // 密码
      password: 'root',
      // 数据库名
      database: 'cms',
    },
    // 是否加载到 app 上,默认开启
    app: true,
    // 是否加载到 agent 上,默认关闭
    agent: false,
  };
//关闭csrf
  config.security={
     csrf:false
  }

 4。编写路由

'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
  router.resources('user', '/user', controller.user);
};

  5.编写控制器

'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  async create() {
    const { ctx } = this;
    ctx.body = 'hi, create';
  }
  async update() {
    const { ctx } = this;
    ctx.body = 'hi, update';
  }
  async destroy() {
    const { ctx } = this;
    ctx.body = 'hi, destroy';
  }
}

module.exports = UserController;

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/guangzhou11/p/10520503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值