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)
相关推荐
新手村领路人18 小时前
Firefox自定义备忘
前端·firefox
乖女子@@@19 小时前
css3新增-网格Grid布局
前端·css·css3
百思可瑞教育19 小时前
使用UniApp实现一个AI对话页面
javascript·vue.js·人工智能·uni-app·xcode·北京百思可瑞教育·百思可瑞教育
伐尘19 小时前
【CE】图形化CE游戏教程通关手册
前端·chrome·游戏·逆向
不想吃饭e19 小时前
在uniapp/vue项目中全局挂载component
前端·vue.js·uni-app
非凡ghost20 小时前
AOMEI Partition Assistant磁盘分区工具:磁盘管理的得力助手
linux·运维·前端·数据库·学习·生活·软件需求
UrbanJazzerati20 小时前
前端入门:margin居中、border、box-radius、transform、box-shadow、mouse事件、preventDefault()
前端·面试
蝎子莱莱爱打怪20 小时前
🚀🚀🚀嗨,一起来开发 开源IM系统呀!
前端·后端·github
Enddme20 小时前
《前端笔试必备:JavaScript ACM输入输出模板》
前端·javascript·面试
前端鱼20 小时前
前端面试中值得关注的js题
前端·面试