Introduction of 0.

  • Project name: Node.js MVC framework based on koa.js.
  • Warehouse address: github.com/zhaotoday/l… .
  • Director: Zhao Jintian.
  • Note: This project references the egg.js framework and the Koa video tutorials shared by iKcamp.

1. Run

Version 1.1. The Node

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

1.2. The command

$NPM install -g pm2 # Install less. Js $NPM install --save less Eslint # develop $NPM run dev # Start project $NPM start # Stop project $NPM run stopCopy the code

2. Specification

2.1. 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, │ │ ├─ ├─ Intermediate │ ├─ Public ├─ Router URL │ ├─ utils │ ├─ 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

2.2. Adding a custom object

In order to improve the development efficiency, some custom objects are artificially mounted to app and named with $prefix to distinguish them from koa.js built-in objects.

  • App.$consts: Constants and configurations
  • $helpers app.$helpers
  • App.$model: Common model object
  • App.$Service: Service base class
  • App.$Controller: Base Controller class
  • $models: A collection of models
  • App.$services: Collection of services
  • App.$controllers: Collection of controllers

2.3. The sample

Full example code: github.com/zhaotoday/l… .

2.3.1. Model

src/app/models/articles.js

module.exports = app => {
  const {ID, SHORT_RELATED_ID, NAME, TITLE, SUBTITLE, DESCRIPTION, CONTENT, PICTURES, ORDER} = app.$model.columns

  return app.$model.define('articles', {
    id: ID,
    category_id: SHORT_RELATED_ID,
    author: NAME,
    title: TITLE,
    subtitle: SUBTITLE,
    description: DESCRIPTION,
    content: CONTENT,
    pictures: PICTURES,
    order: ORDER
  })
}Copy the code

2.3.2. Service

src/app/services/articles.js

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

2.3.3. Controller

src/app/controllers/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

2.3.4. View

src/app/views/articles.ejs

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

2.3.5. API

src/app/controllers/apis/v1/articles.js

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

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

2.3.6. Routing

src/router/routes/articles.js

module.exports = (app, router) => {
  router.get('/articles', app.$controllers.articles.index)
}Copy the code

2.4 extensions

2.4.1. Constants and configurations

src/extends/consts.js

// MySQL database configuration const DB = {database: 'hzzww0n', username: 'hzzww0n_f', password: 'AAAAAA111111 ', options: {host: 'wvort936.669.dnstoo.com', port: 4024, dialect: 'mysql', define: {specify: True}}} // Redis const Redis = {} // JWT const JWT = {secret: 'jwt_secret', expiresIn: '5h' } module.exports = app => { return { PORT, DB, REDIS, JWT } }Copy the code

2.4.2. Auxiliary methods

src/extends/helpers.js

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

Reference 3.

3.1. The document

Articles 3.2.

3.3. Safety