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

# install dependencies
$ npm install

# JS code validation
$ npm run eslintfix
$ npm run eslint

# development
$ npm run dev

# Start a project
$ npm start

# Stop the project
$ npm run stop
Copy 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(a)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.

  • Koa Framework tutorial
  • Koa2 Advanced Study notes
  • Koa2 file upload and download
  • React server render with isomorphism
  • Koa implements JWT certification
  • PM2 Practical Guide
  • Node.js uses Sequelize to operate MySQL
  • Compare Sequelize with MySQL
  • Explains the principle and configuration of nginx reverse proxy
  • JWT user authentication with back-end separation
  • Basic configuration and use of nodemon

3.3. Safety

  • How can I defend against common Web attacks
  • Web Security Series – XSS attacks
  • How to make the front end safer? XSS attack and defense details
  • Must read – Guard against CSRF cross-site request forgery
  • SQL injection for Web security
  • Web security – XSS
  • Filtering HTML by whitelist (against XSS attacks)