Egg introduction guide, you can definitely use it

Familiar Web development frameworks such as Express and KOA, as classic node technology applications, used to be the best choice for Node development, but are there any more flexible and reliable frameworks to replace them? The answer is egg.

Alibaba was one of the first companies to use Node.js as the basic technology support. The emergence of Node made it possible for PHP/Java mode to be replaced. Until the 1.0 version of Egg was launched, this mode became Node/ Java. As an “enterprise-level Web infrastructure framework”, it was born not for Alibaba’s KPI, but as the core infrastructure of Alibaba’s Node.js application.

I. Comparison between Egg and other Frames:

Comparison to KOA: KoA was created by the same people behind Express. It was designed to get rid of the outdated callbacks in Express and to introduce more powerful async/await mechanisms and plug-ins and middleware mechanisms in order to produce a faster Web server with a smaller size and more elegant syntax. However, for enterprise-oriented application framework, this is far from enough. The development mode of multi-person collaboration should be in the form of contract more than configuration to ensure that the development process will produce less communication costs and more efficient coding efficiency. And that’s at the heart of the Egg. Eggs come with their own set of conventions and specifications, so let’s take a look at the project directory generated with an egg.The app contains the business logic and view layer for the entire project. These layers include the middleware, Controller, Model, Router, Service, and so on. These layer files don’t have the heady folders of other frameworks. Instead, it adds a bit of clarity to the coder’s world in a clearer way. Take a look at the convention specification under the APP:

How to build an Egg project

1. Install the Node environment

Running environment: LTS version is recommended. The minimum requirement is 8.x.

2. Creating folders can be done manually or on the command line.

mkdir egg-example && cd egg-example

Initialize the Egg project

npm init egg –type=simple

Then you can see:4. Perform dependency installation

npm i

5. Start the project

npm run dev

At this point a simple egg project is created.

How to use egg link database

There are two methods of linking eggs to databases: Mongo and mysql. Egg does not have a built-in database linking method, so we need to introduce third-party plug-ins, and egg’s support for plug-ins is very powerful.

1. Link egg to Mongo database

In the mongo database, the link to the egg is as follows:

  1. The installation
npm i egg-mongo-native --save
Copy the code
  1. Enable the plugin in the plugin file

{app_root}/config/plugin.js exports.mongo = {enable: true, package: ‘egg-mongo-native’};

  1. Configure the database in config.default.js
// {app_root}/config/config.default.js 
exports.mongo = { 
client: { 
 host: 'host'.port: 'port'.name: 'test'.user: 'user'.password: 'password'.options: {},}}Copy the code

Set the port number and host name based on the actual situation. The host of mongo is localhost and the port number is 27017 4. A brief introduction to the specific application: Search:

 let result=await'order'); 
 let result=await'cate', {query: {"name":zhangsan"});
Copy the code


let result = await'cate', {doc: {"title":"Classification"}});
Copy the code


const result = await'cate', { filter: {"title":"Classification"}, update: {$set: {"title":"11"}}});Copy the code


const result = await'cate', {filter: {"title":"1111"}})
Copy the code

In the actual development we will have some tools, convenient operation of the database and the data to add, delete, change and check. Such as mongones.

2. Link egg to mysql database

Link to mysql using egg-mysql

  1. The installation
 npm i --save egg-mysql
Copy the code
  1. Enable the plugin in the plugin file

{app_root}/config/plugin.js exports.mysql = {

enable: true, package: ‘egg-mysql’, };

  1. Configure the database in config.default.js
// {app_root}/config/config.default.js 

> exports.mysql = {   // Single database configuration
>  client: {
>     // host
>     host: '',
>     / / the port number
>     port: '3306',
>     / / user name
>     user: 'test_user',
>     / / password
>     password: 'test_password',
>     // Database name
>     database: 'test',},// Whether to load to app, app is enabled by default: true, // Whether to load to agent, agent is disabled by default: false,};

Copy the code
  1. A brief introduction to the specific application:

Looking for:

 const post = await'posts', { id: 12 });
Copy the code

Search condition restrictions:

const results = await'posts', { // Search the POST table
  where: { status: 'draft'.author: ['author1'.'author2']},/ / the WHERE condition
  columns: ['author'.'title'].// The fields of the table to be queried
  orders: [['created_at'.'desc'], ['id'.'desc']], // Sort
  limit: 10.// Return the data quantity
  offset: 0.// The data offset
Copy the code


const result = await'posts', { title: 'Hello World' });
Copy the code


const result = await'posts', ‘hi’); 
Copy the code


const result = await'posts', {
  author: 'fengmk2'});Copy the code