1. Configuration related

1. Open terminal input

 sudo npm install -g koa-generator
Copy the code

Sudo must be added to the MAC otherwise the message does not have permission

2. Create projects

 koa2 project
Copy the code

3, CD project to enter the installation dependency

npm install
Copy the code

4. Install required components

npm i jsonwebtoken koa-jwt koa-mysql-session koa-session-minimal koa2-cors md5 moment mysql save --save
Copy the code

5. Install Sequelize to configure the database

npm install sequelize --save
Copy the code

6. Create a config folder to store database connections

Const config = {port: 3000, database: {database: 'XXX ', // database USERNAME: 'root', // user PASSWORD:' XXX ', // PASSWORD port: '3306', // port HOST: '121212' // service IP address}, secret: 'jwt_secret'} module.exports = configCopy the code

Sequelize automatically generates tables to MySQL. Sequelize automatically generates tables to MySQL

Const Sequelize = require(' Sequelize ')const {dbName, user, password, host, port} = require('./config'). Database Name of the linked database // Second parameter: account of the database // third parameter: account password // fourth parameter: Const sequelize = new sequelize (dbName, user, password, {dialect: dialect); Mysql2 host, port, logging:true; // The default value is true; // Whether to display specific mysql operations in the console. '+08:00', // timestamps: False false will not generate createdAt and updatedAt if you set it to false and if you set it to true and if you set it to true and if you set it to true and add deletedAt, UpdatedAt,createdAt sets the default property name to true and // menu sets all camel name field names to define: {timestamps: true, paranoid: true, createdAt: 'created_at', updatedAt: 'updated_at', deletedAt: 'deleted_at', underscored: true }})// force: True When we add a field, the database will automatically delete the original table and update the field. This can be set to true in the development phase. Sync ({force: true})module.exports = {sequelize} sequelize.sync({force: true})module.exports = {sequelize}Copy the code

8. NPM run start http://localhost:3000 is successfully opened

Hello Koa 2!

9. Change the router without going to app.js to register the route one by one so that we can directly iterate through the registration and replace the code directly

index.js

const fs = require('fs'); module.exports = (app) => { fs.readdirSync(__dirname).forEach(file => { if (file === 'index.js') { return; } const route = require(`./${file}`); app.use(route.routes()).use(route.allowedMethods()); }); }Copy the code

App. Js introduced routes

const routing = require('./routes');

routing(app);
Copy the code

Create a controller folder and create a users.js file

Module.exports = new UserCtl(); class UserCtl{index(CTX){ctx.body = 'user list'}}module.exports = new UserCtl();Copy the code

11, Modify the Users of the routers to require the created users router. Modify the router.get request method

const Router = require('koa-router'); const router = new Router()const {index } = require('.. /controller/users')router.get('/', index)module.exports = router;Copy the code

Rerun the project list and you will see the list of users displayed

Patch:

You can install Nodemon so that you do not need to restart the interface after each modification

cnpm i nodemon
Copy the code

You need to modify package.json

Start should be node

  "scripts": {    "start": "nodemon bin/www",    "dev": "./node_modules/.bin/nodemon bin/www",    "prd": "pm2 start bin/www",    "test": "echo \"Error: no test specified\" && exit 1"  },
Copy the code

Then run the project using NPM run start