Koa project to build modular routing and practice

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_40629244/article/details/100923296

Koa is a free and flexible back-end frame, ideal for individual project development, in the development, will generally generate an initial project with Koa-generator application generator, the generator main application defines a number of folders, and routing achieve a modular, in fact, we can also customize an application builder for easy self-expansion.

1. Custom Folder

(1) .module folder. Mainly used to store some public modules used in the project development.

(2) .statics folder. Mainly used to store static files stored in project development, according to the static document classification, they can add some sub-folders for storing images such as images, css styles folder used to store files, js folder used to store some of the front end interaction.

(3) .views folder. The main template file used to store front-end rendering, according to project needs, you can add some subfolders, such as public folders public head and tail for storing multiple pages.

 

2. Modular Routing

First, the introduction of modular sub-routes predefined in app.js, the routing address and the access it corresponds binding.

const Koa = require('koa');
const router = require('koa-router')();
const render = require('koa-art-template');
const path = require('path');
const app = new Koa();

render(app, {
    root: path.join(__dirname, 'view'),
    extname: '.html',
    debug: process.env.NODE_ENV !== 'production'
});

// 1.引入模块化的子路由
var admin = require('./routes/admin.js');
var api = require('./routes/api.js');

// 2.绑定路由
router.use('/admin', admin);
router.use('/api', api);


router.get('/', (ctx) => {
    ctx.body = "这是一个首页"
});

app.use(router.routes());
app.use(router.allowedMethods());
app.listen(8008);

Routing sub codes introduced defined above, the admin example, since the route is represented by a background amin management system, there will be a lot of the interface, it can be subdivided several sub-routes.

var router = require('koa-router')();

// 3.引入子路由
var user = require('./admin/user.js');
var focus = require('./admin/focus.js');

router.get('/', (ctx) => {
    ctx.body = '这是后台管理系统首页';
});

// 4.使用子路由
router.use('/user', user);
router.use('/focus', focus);


router.get('/news', (ctx) => {
    ctx.body = '新闻管理'
});

module.exports = router.routes();

The following is the definition of sub-admin user routing, including add, delete, change, search, in fact, all of these interfaces are operating.

var router = require('koa-router')();

router.get('/', (ctx) => {
    ctx.body = '用户首页'
});

router.get('/add', (ctx) => {
    ctx.body = '用户增加'
});

router.get('/edit', (ctx) => {
    ctx.body = '编辑用户'
});

router.get('/delete', (ctx) => {
    ctx.body = '删除用户'
});

module.exports= router.routes();

 

Guess you like

Origin blog.csdn.net/weixin_40629244/article/details/100923296