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