Home  >  Article  >  Web Front-end  >  Is egg.js node?

Is egg.js node?

藏色散人
藏色散人Original
2022-12-29 14:51:501926browse

egg.js is a node framework, a node.js framework inherited from Koa; the egg.js framework is different from basic frameworks such as exporess and koa. Egg.js is refined and encapsulated at the application level to make it more Close to business scenarios and get started faster.

Is egg.js node?

#The operating environment of this tutorial: Windows 10 system, egg.js v2.0.0 version, Dell G3 computer.

Is egg.js node?

yes.

Understand egg.js

Egg is a node.js framework inherited from Koa. Different from basic frameworks such as exporess and koa, egg.js is refined and encapsulated at the application level, making it closer to business scenarios and faster to get started.

Egg is developed in accordance with the agreement, adhering to "convention is better than configuration", and the team collaboration cost is low

Installation

npm init egg
npm i
npm run dev

A basic API is roughly composed of It consists of routing, obtaining request parameters, logical processing, and returning response data

routing

app/router.js is used to configure URL routing rules

router.get("/", controller.home.index);
// 当访问GET '/' ,app/controller/home.js 下的 index 方法会执行
router.post("/create", controller.user.create);
// 当访问POST '/create' ,app/controller/user.js 下的 create 方法会执行

Get request parameters

This.ctx.query gets the following parameters in the URL?

// GET /posts?category=egg&language=node
// app/controller/post.js
class PostController extends Controller {
  async listPosts() {
    const query = this.ctx.query;
    // {
    //   category: 'egg',
    //   language: 'node',
    // }
  }
}

this.ctx.params gets the dynamic parameters in the route

// app.get('/projects/:projectId/app/:appId', controller.app.listApp);
// GET /projects/1/app/2
class AppController extends Controller {
  async listApp() {
    const params = this.ctx.params;
    // {
    //   projectId: '1',
    //   appId: '2'
    // }
  }
}

this.ctx.request.body Get body parameters

// POST /api/posts HTTP/1.1
// Host: localhost:3000
// Content-Type: application/json; charset=UTF-8
//
// {"title": "controller", "content": "what is controller"}
class PostController extends Controller {
  async listPosts() {
    const body = this.ctx.request.body;
    // {
    //   title: 'controller',
    //   content: 'what is controller'
    // }
  }
}

Return response data

this.ctx.bodyReturn response data

class ViewController extends Controller {
  async show() {
    // 返回Content-Type为application/json的body
    this.ctx.body = {
      name: "egg",
      category: "framework",
      language: "Node.js",
    };
  }
  async page() {
    // 返回Content-Type为text/html的body
    this.ctx.body = "<html><h1>Hello</h1></html>";
  }
}

Use mysql database

Install mysql plug-in

npm i egg-mysql

Configuration

// config/plugin.js
exports.mysql = {
  enable: true,
  package: "egg-mysql",
};
// config/config.${env}.js
exports.mysql = {
  // 单数据库信息配置
  client: {
    // host
    host: "localhost",
    // 端口号
    port: "3306",
    // 用户名
    user: "root",
    // 密码
    password: "root",
    // 数据库名
    database: "database",
  },
};

Use

// 查找id 为 ${uid}的用户
await this.app.mysql.get("users", { id: uid });

Process business logic

It is recommended that the business logic be placed in app/service, including database operations.

// app/service/user.js
const Service = require("egg").Service;
class UserService extends Service {
  async find(uid) {
    // 假如 我们拿到用户 id 从数据库获取用户详细信息
    const user = await this.app.mysql.get("users", { id: uid });
    return user;
  }
}
module.exports = UserService;

Afterwards, the data obtained by the Service layer can be obtained through the Controller.

// app/controller/user.js
class UserController extends Controller {
  async info() {
    const ctx = this.ctx;
    const userId = ctx.params.id;
    // 调用service层的user下的find方法
    const user = await ctx.service.user.find(userId);
    ctx.body = user;
  }
}

Basic CURD statements can use the create, get, select, update, delete methods

To directly execute sql statements, you can use the query method

Control of transactions

egg.js 官网:https://www.eggjs.org/zh-CN/

Recommended learning: "node.js video tutorial"

The above is the detailed content of Is egg.js node?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn