Home > Web Front-end > Front-end Q&A > Is egg.js node?

Is egg.js node?

藏色散人
Release: 2022-12-29 14:51:50
Original
1948 people have browsed it

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
Copy after login

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 方法会执行
Copy after login

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',
    // }
  }
}
Copy after login

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'
    // }
  }
}
Copy after login

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'
    // }
  }
}
Copy after login

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>";
  }
}
Copy after login

Use mysql database

Install mysql plug-in

npm i egg-mysql
Copy after login

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",
  },
};
Copy after login

Use

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

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;
Copy after login

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;
  }
}
Copy after login

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/
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template