Basic use of Egg.js's first learning--controller and router

Source: Internet
Author: User

Today I learned the use of egg's most basic controller and router.

First, what are controllers and router? Controller is the controller layer in the classic MVC (Module,view,controller) architecture, which is mainly used to solve the actual business logic.

Router is primarily used to distribute requests from the page, and then hand over the request to a controller. Here's a little chestnut.

1 module.exports = App = 2   app.get ('/', ' Render.ejs '); 3 };

The first argument to the second line here is '/' means that the requested path is the root directory, and the second parameter pair controller is called when the method is GET.

Egg has a particularly good place, that is, the directory structure is very clear, the second argument actually means that the call path is ' App/controller/render.js ' in the Ejs this method

1 function *(CTX) {2   yield ctx.render (' Hello.ejs ', {3     data: Ctx.params.id + ctx.query.id,4    _csrf:ctx._csrf5  }); 6 };

The first line, I exposed is a name called Ejs method, as long as the root directory access, routing will handle the task of this request to do this method.

The meaning of this method is to enable a template, called ' Hello.ejs ', and its actual position is under the app/view/, which is also the framework of the dead.

Basic use of Egg.js's first learning--controller and router

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.