eggjs初用

npm i egg-init -g // 全局安装egg.js的脚手架工具egg-init

mkdir testEgg

cd testEgg

egg-init --type=simple // 初始化egg项目

npm install  // 安装依赖

npm run dev // 启动服务

浏览器打开:http://localhost:7001

主要项目目录功能

app文件夹: 项目开发文件像src这样的功能。
config文件夹:项目的配置目录,项目和服务端的配置都在这里边进行设置。
logs文件夹:日志文件夹。

在app目录下,egg通常会有下面的文件:
controller文件夹:控制器,渲染和简单的业务逻辑都会写道这个文件里。配置路由时也会用到(路由配置需要的文件都要写在控制器里)。
public文件夹: 公用文件夹,放置静态资源,可选。
router.js文件: 项目的路由配置文件,当用户访问服务的时候,在没有中间件的情况下,会先访问router.js文件。
service文件夹: 用于编写业务逻辑层,可选,建议使用(涉及到数据库如sql MongoDB等)。
view文件夹: 模板文件夹,相当于表现层的专属文件夹,用于放置模板文件,可选,由模板插件约定(模板渲染用)。
extend文件夹: 用于框架的扩展,可选;
schedule文件夹: 用于定时任务,可选
middleware文件夹:中间件文件夹,用来写中间件的,比如最常用的路由首位。

常用的接口有

RESTful是目前最流行的网络应用程序设计风格和开发方式;列举四种请求常用方式

GET(SELECT) ----> 从服务端取出资源
POST(CREATE) ----> 在服务器新建资源
PUT(UPDATE) ----> 在服务器更新资源
DELETE(DELETE) ----> 从服务器删除资源

/*
 * @Description: controller/home.js
 * @Version: 2.0
 * @Autor: lhl
 * @Date: 2020-04-16 14:12:13
 * @LastEditors: lhl
 * @LastEditTime: 2020-04-17 14:25:28
 */
'use strict';

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

class HomeController extends Controller {

  // 自己写个list方法  ---> router.js下面配置  router.get('/', controller.home.index); -->  访问 http://localhost:7001
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  // 自己写个list方法  ---> router.js下面配置  router.get('/list', controller.home.list); -->  访问 http://localhost:7001/list
  async list() {
    const { ctx } = this;
    ctx.body = '<h1>自定义接口内容xxxxxx</h1>';
  }
}

module.exports = HomeController;
/*
 * @Description: 接口配置文件 router.js
 * @Version: 2.0
 * @Autor: lhl
 * @Date: 2020-04-16 14:12:13
 * @LastEditors: lhl
 * @LastEditTime: 2020-04-17 09:22:09
 */
'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
  router.get('/list', controller.home.list);
};
/*
 * @Description: config/config.default.js
 * @Version: 2.0
 * @Autor: lhl
 * @Date: 2020-04-16 14:12:13
 * @LastEditors: lhl
 * @LastEditTime: 2020-04-17 14:46:49
 */
/* eslint valid-jsdoc: "off" */

'use strict';

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};
  // 数据库链接配置
  config.mysql = {
    // database configuration
    client: {
      // host
      host: 'localhost',
      // port
      port: '3306',
      // username
      user: 'root',
      // password
      password: 'root',
      // database
      database: 'test',    
    },
    // load into app, default is open
    app: true,
    // load into agent, default is close
    agent: false,
  };

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1587017528543_2972';

  // add your middleware config here
  config.middleware = [];

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  return {
    ...config,
    ...userConfig,
  };
};
/*
 * @Description: config/plugin.js
 * @Version: 2.0
 * @Autor: lhl
 * @Date: 2020-04-16 14:12:13
 * @LastEditors: lhl
 * @LastEditTime: 2020-04-17 14:48:49
 */
'use strict';

/** @type Egg.EggPlugin */
module.exports = {
  // had enabled by egg
  // static: {
  //   enable: true,
  // }
   // 配置数据库 mysql
  mysql:{
    enable:true,
    package:'egg-mysql',
  }
  
};

 

posted @ 2020-04-17 14:54  蓝色帅-橙子哥  阅读(415)  评论(0编辑  收藏  举报