Introduction of 0.

  • Project name: Node.js MVC framework based on koa.js.
  • Warehouse address: github.com/zhaotoday/k… .
  • Person in charge: Zhao Jintian @zhaotoday.

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 run eslintfix $NPM run eslint # Develop $NPM run dev # start project $NPM start project # 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, Back to the client's view layer │ │ │ ├ ─ core core code │ │ ├ ─ controller. The js controller base class │ │ ├ ─ model. The js model base class │ │ └ ─ service. Js service base class │ │ │ ├ ─ middlewares │ ├─ ├─ ├─ ├─ ├─ ├─ ├.js middleware │ ├─ ├─ ├─ ├─ ├.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. Customize mount objects

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.

  • $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

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

Reference 3.

3.1. 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

Articles 3.2.

3.3. Safety