Based Koa2 + blog backend framework of mongoDB

The main framework: koa2 family bucket + mongoose + pm2.

Before reading suggestions to clone a project with the local food, or they will see foggy.
Project Address: https://github.com/YogurtQ/koa-server .
If wrong, please correct me. If in doubt, welcome exchange.

We strongly recommend students to still use express koa2, koa2 express this is to build the original cast, familiar with the express words to get started quickly, and es6 / es7 more you use the grammar really cool. If you are a novice, then Learndirect koa2 on the right.

First, install

+ v7.6.0 Node.js
NPM + V3.x
be installed pm2

Global installed pm2
npm i -g pm2

Installation depends
npm i

Start development
npm run dev

Start production
npm run build

Start test
npm run test

Second, the project structure

├─.gitignore                // 忽略文件配置
├─app.js                    // 应用入口
├─config.js                 // 公共配置文件
├─ecosystem.config.js       // pm2配置文件
├─package.json              // 依赖文件配置
├─README.md                 // README文档
├─routes                    // 路由
|   ├─private.js                // 校验接口
|   ├─public.js                 // 公开接口
|   └view.js                    // 页面接口
├─models                    // 模型
|   ├─index.js                  // 配置
|   └user.js                    // schema
├─controllers               // 操作业务逻辑
|      ├─index.js               // 配置
|      ├─login.js               // 登录
|      └test.js                 // 测试
├─services                  // 业务
|      ├─index.js               // 配置
|      └user.js                 // 用户
├─middlewares               // 中间件
|      ├─cors.js                // 跨域中间件
|      ├─jwt.js                 // jwt中间件
|      ├─logger.js              // 日志打印中间件
|      └response.js             // 响应及异常处理中间件
├─logs                      // 日志目录
├─lib                       // 依赖库
|  ├─error.js                   // 异常处理
|  ├─baseDAO.js                 // 基础业务类
|  └mongoDB.js                  // mongoDB配置
├─bin                       // 启动目录
|  ├─.sh/.bat                   // 启动和停止批处理文件
|  └www                         // 启动文件配置

Third, Item Description

1.routes route

  • public do not need to check, direct access
  • private login required, after authentication via accessible
  • view front page routing, non-essential

2.views page

  • Page directory, the default home page is index.html, can be modified routes / view, a template engine for the use of self-install
  • Now it has been the practice, separating the front and rear ends, the front end of the file into the proxy server. As dist files will be thrown into the package nginx in vue project, but in order to ensure the integrity of the frame, or to retain the views directory

3.services business

  • Index.js action is registered automatically scan the current business class folder, but does not scan js files in subfolders, so businesses are usually placed directly on the services folder, if there are special circumstances, the new subfolders and discretion, such as directories under the frame fileService
  • writing the service class are used, and therefore needs support node version, and are inherited from the class BaseDao lib directory, BaseDao are some basic methods, thereby avoiding rewriting the find / delete the like, other if necessary, in self-service extensions
  • service exported class names to be consistent with the js file name

4.models model

  • index.js file with the services, if there are special needs, create subfolders and discretion
  • Schema of the model substantially mongoose, mongoose method according to define field types, validation rules,

5.controllers controller

  • index.js file with the services, if there are special needs, create subfolders and discretion
  • A method for routing a corresponding controller, and due to the need to operate the database koa2 employed async / await, so the directory is a method async method, note asynchronous write problem.
  • Write format controller.js method is: " 'METHOD url': async function ()", namely: "" Request Type Request Address': Processing Method ", Method Capitalize, unified at the interface api '/ api' beginning

6.pm2

  • pm2 mainly used to guard applications, are interested in self-learning
  • pm2 not koa project must, but due to the use of scripts package.json pm2 command, so the need to install or modify their own scripts

7. Other

  • Other catalog description see chapter project structure, without too much explanation
  • See profile config.js

Guess you like

Origin www.cnblogs.com/yogurtq/p/12155623.html