Introduction to the

  • Project: Node.js MVC framework based on koa.js.
  • Warehouse address: github.com/zhaotoday/l… .

version

  • V1.1.4 [2018-12-29] supports URL Rewrite;
  • V1.1.0 [2018-12-27] Support Redis database, operating Redis through ORM framework JugglingDB;
  • V1.0.9 [2018-12-23] supports synchronization of MySQL database model;
  • V1.0.8 [2018-12-20] supports automatic routing based on the controllers directory structure without manual configuration.
  • V1.0.5 [2018-12-10] Cancel koA-JWT middleware and perform JWT check in controller basic class;

to-do

  • Synchronize MySQL data to Redis;

The sample

The sample code

Check out the example folder.

Administrative backend code

Please visit the github.com/zhaotoday/i… .

Online sample

Please visit admin.cmsx.cn/.

Account: admin Password: 123456Copy the code

Note: Press F12 to open Chrome Developer Tools to view interface requests.

run

Node. Js version

Koa2 uses new syntax such as async/await, please ensure node.js version 7.6 or later.

The command

$NPM install -g pm2 # Install less. Js $NPM install --save less Eslint # $NPM run dev # Start project $NPM run start:dev # Synchronize database model $NPM run sync-Models :dev # Stop project $NPM run stopCopy the code

Note :dev indicates that the configuration of the development environment is invoked when the command is executed. Dev indicates the development environment, test indicates the test environment, beta indicates the pre-production environment, and prod indicates the production environment.

The directory structure

Overall directory structure

├ ─ SRC source │ ├ ─ app business code │ │ ├ ─ controllers controller: used to resolve user input, processing to return the corresponding results of │ │ ├ ─ models: the model is used to define data models │ │ ├ ─ services services: ├ ─ ├ ─ sci-08 (0 folders, 2 files) ├ ─ sci-08 (0 folders, 2 files) Used to place template files, Back to the view layer │ │ ├─ Config │ ├─ extends │ ├─ Middlewares Middleware │ ├─ Public Static Resources │ ├─ Redis Redis Database │ ├─ Router URL ├─ ├─ index.js For custom startup initialization, such as starting HTTPS, │ ├─ Nodemon. Json Nodemon Configuration file ├─ Package. json NPM Configuration file ├─ process. json PM2 configuration fileCopy the code

Configures the directory structure

├─ Config │ ├─ Base.js Basic │ ├─ test.js Test environment │ ├─ bea. js Pre - production environment │ ├─ production.js Production Environment ConfigurationCopy the code

Extends a directory structure

├ ─ extends extension │ ├ ─ controller. The controller was extended │ js ├ ─ helpers. Js extensions to the auxiliary function │ ├ ─ the initialize. Js initialization │ └ ─ service. Js for extended serviceCopy the code

Controllers directory structure

├─ Controllers │ ├─ ├─ ├─ ├─ ├─ ├─ exercises, ├─ exercises, exercises, exercises, exercises, exercises, exercises, exercises, exercises Articles interface controller │ │ │ └ ─ files. The js files interface controller │ │ │ │ │ ├ ─ public public interface (call) without authentication │ │ │ │ │ └ ─ some app - an application's interface │ │ │ ├ ─ │ ├─ ├─ class.js files │ ├─ ├─ class.js filesCopy the code

Views Directory structure

├ ─ views view │ ├ ─ PC PC │ │ ├ ─ articles. The ejs articles page view │ │ └ ─ files. The ejs files page view │ │ │ ├ ─ mobile mobile terminal │ │ ├ ─ ├ ─ ├ ─ impCopy the code

extension

Some extensions to koa.js are named with the $prefix to distinguish them from koa.js built-in objects.

App.$config: configure app.$module: load the built-in module app.$resources: generate RESTful route. $models: controllers.$Services: controllers.$Models: controllersCopy the code

To extend helper functions, create SRC /extends/helpers.js:

module.exports = app => {
  return {
    myFunc () {}
  }
}Copy the code

The sample code

model

src/app/models/articles.js

module.exports = app => { const { STRING, TEXT, INTEGER } = app.$Sequelize return app.$model.define('articles', { id: { type: INTEGER(10).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: 'ID' }, title: { type: STRING(200), allowNull: false, comment: 'header'}, content: {type: TEXT('long'), comment: 'content'}})}Copy the code

service

src/app/services/articles.js

module.exports = app => { return class extends app.$Service { constructor () { super() this.model = app.$models.articles }}}Copy the code

Page controller

src/app/controllers/web/articles.js

module.exports = app => {
  const service = app.$services.articles

  return class extends app.$Controller {
    async index (ctx, next) {
      await ctx.render('articles', {
        items: await service.find({ offset: 0, limit: 10 })
      })
    }
  }
}Copy the code

The API controller

src/app/controllers/api/v1/public/articles.js

module.exports = app => {
  const service = app.$services.articles

  return class extends app.$Controller {
    async index (ctx, next) {
      ctx.send({
        status: 200,
        data: await service.find({ offset: 0, limit: 10 })
      })
    }
  }
}Copy the code

view

src/app/views/pc/articles.ejs

<%- JSON.stringify(items) %>Copy the code

routing

src/router/index.js

const router = require('koa-router')() module.exports = app => { router.get('/', app.$controllers.web.home.index) router.get('/articles/:id? ', app.$controllers.web.articles.index) app.use(router.routes()).use(router.allowedMethods()) }Copy the code

reference

The document

  • Koa website
  • Koa Chinese website
  • Chinese version of Koa document
  • The Node Koa2 of actual combat
  • Sequelize document
  • Chinese version of the Sequelize document
  • EJS website
  • EJS official website Chinese version
  • EJS Chinese document
  • EJS template language is used
  • PM2 website
  • Node.js best practices

The article

MySQL

Redis

security

Other reference