• egg.js学习记录


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

    1. pnpm create egg //创建项目
    2. //选择simple就行
    3. pnpm i //下载插件
    4. pnpm run dev //运行项目

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

    1. pnpm i egg-cors //跨域依赖
    2. pnpm i egg-mysql //数据库依赖
    3. pnpm i egg-scripts //部署

    3.修改配置文件 plugin.js

    1. module.exports = {
    2. // had enabled by egg
    3. // static: {
    4. // enable: true,
    5. // }
    6. mysql:{
    7. enable:true,
    8. package:'egg-mysql'
    9. },
    10. cors:{
    11. enable:true,
    12. package:'egg-cors'
    13. }
    14. };

    4.配置config.default.js

    1. module.exports = appInfo => {
    2. /**
    3. * built-in config
    4. * @type {Egg.EggAppConfig}
    5. **/
    6. const config = exports = {};
    7. // use for cookie sign key, should change to your own and keep security
    8. config.keys = appInfo.name + '_1709895338474_7197';
    9. // add your middleware config here
    10. config.middleware = [];
    11. // add your user config here
    12. const userConfig = {
    13. // myAppName: 'egg',
    14. };
    15. //关闭CSRF验证 跨域请求 不进行同源验证
    16. config.security = {
    17. csrf:{
    18. enable:false
    19. }
    20. }
    21. config.cors = {
    22. origin: '*',
    23. allowMethods: 'GET,POST,PUT,DELETE' //设置允许访问的请求方式
    24. }
    25. //配置数据库连接
    26. config.mysql = {
    27. app:true,
    28. client:{
    29. host:'localhost', //数据库地址
    30. port:'3306', //数据库端口号
    31. user:'root', //数据库用户名
    32. password:'123456', //数据库密码
    33. database:'mitest', //要连接的数据库
    34. }
    35. }
    36. return {
    37. ...config,
    38. ...userConfig,
    39. };
    40. };

    5.配置service层

    1. const Service = require("egg").Service;
    2. class CustomerService extends Service {
    3. //4.登录(根据客户手机号码和密码进行查询)
    4. async selectCustomerByTelIdByPass(customer) {
    5. let result;
    6. try {
    7. //调用数据库实现查询
    8. let sql = `select * from customer where telId=${customer.telId} and password=${customer.password}`;
    9. result = await this.app.mysql.query(sql);
    10. } catch (error) {
    11. console.log(error)
    12. }
    13. return result;
    14. }
    15. }
    16. module.exports = CustomerService;

    6.配置controller层

    1. const { Controller } = require("egg");
    2. class CustomerController extends Controller {
    3. //1.全查询所有的商品分类信息
    4. async selectCustomerByTelIdByPass() {
    5. const { ctx } = this; //对应的application实例 全局
    6. //调用service层方法得到结果 返回给前台
    7. const result = await this.ctx.service.customer.selectCustomerByTelIdByPass(this.ctx.request.body);
    8. ctx.body = result
    9. }
    10. }
    11. module.exports = CustomerController

    7.配置路由层

     router.post('/selectUserByUserByPass' , controller.userinfo.selectUserByUserByPass)

  • 相关阅读:
    SpringBoot_整合PageHelper
    vue3使用less与scss
    PDF转PPT软件哪个好?这几款软件亲测实用
    Nacos 注册中心使用说明
    算法模型总结:单调栈
    联机算法和脱机算法[Alg_001]
    LeetCode-1110. Delete Nodes And Return Forest [C++][Java]
    将目录下的所有pdf文件都转换为对应名字的png图片
    m分别通过GA遗传优化算法对企业不同产品订单生产进行时间优化
    安装Python
  • 原文地址:https://blog.csdn.net/m0_53785610/article/details/136607818