// Router routing // Router is mainly used to describe the specific request URL and assume the correspondence between the Controller to perform an action, the framework agreed app / router.js file is used to unify all routing rules. // through a unified configuration, we can avoid routing rule logic scattered in several places, so an unknown conflict, together we can be more convenient to view the global routing rules. // define Router // App / router.js URL routing rules defined inside // App / router.js module.exports App = => { const { router, controller } = app; router.get('/user/:id', controller.user.info); }; // App / Controller directory achieve the Controller // App / Controller / user.js class the extends the UserController the Controller { async info() { const { ctx } = this; ctx.body = { name: `hello ${ctx.params.id}`, }; } } // This completes a simple Router definition, when the user performs GET /user/123,user.js this method will be executed inside info. // Router detailed description defined // The following is a complete definition of the route, depending on the scene parameters can be freely chosen: router.verb ( 'path-match' , app.controller.action); router.verb('router-name', 'path-match', app.controller.action); router.verb('path-match', middleware1, ..., middlewareN, app.controller.action); router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.action); // verb: user-triggered actions, support get, post and all HTTP methods router.head: HEAD router.options: OPTIONS router.get: GET router.put: PUT router.post: POST router.patch: PATCH router.delete: DELETE router.del: Due to delete is a reserved word, so providing a delete the alias method. router.redirect: URL can redirect processing, such as the root directory and can route users to access our most frequently used to a home. Router - name: alias to a routing setting, and may assist the function pathFor urlFor Helper provided by generating a URL. (Optional) path - match: URL routing path. middleware1: In Router which you can configure multiple Middleware. (Optional) controller: the controller specifies the specific route mapped, there are two writing controller: app.controller.user.fetch: directly specifying a specific controller 'user.fetch' : string can be abbreviated as // Note the Router definition, can support multiple serial execution Middleware Controller must be defined in App / directory of the Controller. A document which may include a plurality Controller defined in the definition of the route, they can. $ {FunctionName} Controller designate corresponding manner by $ {fileName}. Controller support subdirectories in the definition of the route, they can by $ {directoryName (directory name)}. $ {FileName (file name)}. $ {FunctionName (method name)} a way to develop corresponding Controller. // App / router.js Here are some ways route definition: module.exports App = => { const { router, controller } = app; router.get('/home', controller.home); router.get('/user/:id', controller.user.page); router.post('/admin', isAdmin, controller.admin); router.post('/user', isLoginUser, hasAdminPermission, controller.user.create); router.post('/api/v1/comments', controller.v1.comments.create); // app/controller/v1/comments.js }; // parameters named // App / router.js module.exports App = => { // GET parameter splicing http://127.0.0.1:7001/user/123/xiaoming app.router.get ( '/ User / : ID /: name ' , app.controller.user.info); // form post parameter acquisition request app.router.post (' / form ' , app.controller.form.post); }; // app/controller/user.js exports.info = async ctx => { // get ctx.body = `user: ${ctx.params.id}, ${ctx.params.name}`; // 表单post ctx.body = `body: ${JSON.stringify(ctx.request.body)}`; }; // Note: Direct initiate POST request will complain: 'Secret IS Missing' // Cause: The internal framework for a form POST request will verify the value CSRF, so when we form submission, please bring CSRF key submit, refer to preventive security threats csrf // because the security framework built into the plug-egg-security, provides some default security practices, and security plug-in framework is enabled by default, if you need to close some security, enable the item directly property is false. // disposed config / config.default.js in exports.security = {CSRF: to false }; or = {CSRF config.security: to false } // form validation // App / router.js module.exports App = => { app.router.post('/user', app.controller.user); }; // app/controller/user.js const createRule = { username: { type: 'email', }, password: { type: 'password', compare: 're-password', }, }; exports.create = the async CTX => { // if parity error exception will be thrown ctx.validate (createRule); ctx.body = ctx.request.body; }; // multi-route map // App / router.js module.exports App = => { require('./router/news')(app); require('./router/admin')(app); }; // app/router/news.js module.exports = app => { app.router.get('/news/list', app.controller.news.list); app.router.get('/news/detail', app.controller.news.detail); }; // app/router/admin.js module.exports = app => { app.router.get('/admin/user', app.controller.admin.user); app.router.get('/admin/log', app.controller.admin.log); };