Build the project
- Installation of koa – the generator
yarn global add koa-generator
Copy the code
- Use KOA-Generator to generate koA2 project -E using EJES as template
koa2 -e projectname
Copy the code
- Start project
cd project
yarn install
Copy the code
The directory structure
Refactoring directory
Of course, this catalog doesn’t fit our current development style, so we’re going to turn it into the FAMILIAR MVP mode
- The pulic directory is a resource directory that can be left untouched
- The bin directory is an important configuration file and does not move
- Create app directory and create modules admin (for blog background management), INDE (for blog official content), route (for managing routes), db.js (for configuring link database parameters)
- Create a new Controller, Model, View directory for each module so that the MVC model comes out
- Delete files in other directories
Look for good templates
In order to save cost of development, we need to download some good template I used is [amazeUI] (tpl.amazeui.org/content.htm…
) template file. Copy all the static files to the public folder and put the HTML template file into the view of the index module with the EJS suffixYou also need to change the path of static resources in the template file to import them correctly into the public file
Build the route, here to separate the route from the processing logic we put the processing logic in the Controller directory as the Controller layer, and then import it in the Router
Here is how to write the controller layer file. Koa2 uses async/await writing for asynchronous requests. And introduce the Model.
MVC mode V and C has now set up good, the bad M, so first we want to install the Sequelize used to link the database, the following command to install Sequelize, click can go to look at [Sequelize] (www.jianshu.com/p/72a6b80b6…
), I won’t go into too much detail here.
$ yarn add sequelize
Copy the code
Create db.js in the app folder to configure the database
var Sequelize = require('sequelize')
module.exports = new Sequelize('yzgblog'.'yzgblog'.'yzg_blog', {host:'Your database address'.dialect:'mysql'.pool: {
max: 5.min: 0.idle: 10000}})Copy the code
Create new model layers under the Model folder such as Users, blogs, and so on. Introduce db.js configuration
let Sequelize = require('sequelize')
let sequelize = require('.. /.. /db')
let User = sequelize.define('users', {nickname: {type:Sequelize.STRING
},
avater: {type:Sequelize.STRING
},
email: {
type: Sequelize.STRING
}
})
module.exports = User;
Copy the code
At this point, the general framework of the project, the MVC layers are all set up