egg.js学习记录

1.创建egg项目并启动后端服务器

javascript 复制代码
pnpm create egg    //创建项目
//选择simple就行
pnpm i    //下载插件
pnpm run dev    //运行项目

2.安装跨域依赖和数据库依赖

javascript 复制代码
pnpm i egg-cors    //跨域依赖
pnpm i egg-mysql    //数据库依赖
pnpm i egg-scripts  //部署

3.修改配置文件 plugin.js

javascript 复制代码
module.exports = {
  // had enabled by egg
  // static: {
  //   enable: true,
  // }
  mysql:{
    enable:true,
    package:'egg-mysql'
  },
  cors:{
    enable:true,
    package:'egg-cors'
  }
};

4.配置config.default.js

javascript 复制代码
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

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

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

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

  //关闭CSRF验证  跨域请求  不进行同源验证
  config.security = {
    csrf:{
      enable:false
    }
  }

  config.cors = {
    origin: '*',
    allowMethods: 'GET,POST,PUT,DELETE' //设置允许访问的请求方式
  }

  //配置数据库连接
  config.mysql = {
    app:true,
    client:{
      host:'localhost', //数据库地址
      port:'3306', //数据库端口号
      user:'root',   //数据库用户名
      password:'123456', //数据库密码
      database:'mitest',  //要连接的数据库
    }

  }

  return {
    ...config,
    ...userConfig,
  };
};

5.配置service层

javascript 复制代码
const  Service  = require("egg").Service;

class CustomerService extends Service {
    //4.登录(根据客户手机号码和密码进行查询)
    async selectCustomerByTelIdByPass(customer) {
        let result;
        try {
            //调用数据库实现查询
            let sql = `select * from customer where telId=${customer.telId} and password=${customer.password}`;  
            result = await this.app.mysql.query(sql);
        } catch (error) {
            console.log(error)
        }
        return result;
    }
}

module.exports = CustomerService;

6.配置controller层

javascript 复制代码
const { Controller } = require("egg");

class CustomerController extends Controller {
    //1.全查询所有的商品分类信息
    async selectCustomerByTelIdByPass() {
        const { ctx } = this; //对应的application实例  全局
        //调用service层方法得到结果  返回给前台
        const result = await this.ctx.service.customer.selectCustomerByTelIdByPass(this.ctx.request.body);
        ctx.body = result
    }
}

module.exports = CustomerController

7.配置路由层

javascript 复制代码
 router.post('/selectUserByUserByPass' , controller.userinfo.selectUserByUserByPass)
相关推荐
新缸中之脑8 分钟前
Llama 3.2 安卓手机安装教程
前端·人工智能·算法
hmz85611 分钟前
最新网课搜题答案查询小程序源码/题库多接口微信小程序源码+自带流量主
前端·微信小程序·小程序
看到请催我学习18 分钟前
内存缓存和硬盘缓存
开发语言·前端·javascript·vue.js·缓存·ecmascript
blaizeer1 小时前
深入理解 CSS 浮动(Float):详尽指南
前端·css
速盾cdn1 小时前
速盾:网页游戏部署高防服务器有什么优势?
服务器·前端·web安全
小白求学11 小时前
CSS浮动
前端·css·css3
什么鬼昵称1 小时前
Pikachu-csrf-CSRF(POST)
前端·csrf
XiaoYu20022 小时前
22.JS高级-ES6之Symbol类型与Set、Map数据结构
前端·javascript·代码规范
golitter.2 小时前
Vue组件库Element-ui
前端·vue.js·ui
儒雅的烤地瓜2 小时前
JS | JS中判断数组的6种方法,你知道几个?
javascript·instanceof·判断数组·数组方法·isarray·isprototypeof