Using vue+node+koa+sequlize to realize the background management system

Keywords: node.js MySQL JSON Database TypeScript

  1. mkdir AceNode && cd AceNode
  2. vi package.json
  3. Add the following to package.json
{
  "name": "ace-node",
  "version": "1.0.0",
  "description": "backend for ace",
  "main": "./src/app.js",
  "scripts": {
    "start": "nodemon node ./src/app.js",
    "build": "babel ./src/app.js -d dist"
  },
  "keywords": [
    "koa",
    "async"
  ],
  "author": "Who loves who",
  "license": "1.0",
  "dependencies": {
    "@types/async": "^3.0.3",
    "@types/koa": "^2.0.51",
    "@types/koa-bodyparser": "^4.3.0",
    "@types/koa-json": "^2.0.18",
    "@types/koa-logger": "^3.1.1",
    "@types/koa-router": "^7.0.42",
    "@types/koa2-cors": "^2.0.1",
    "@types/mysql": "^2.15.7",
    "async": "^3.1.0",
    "http-proxy-middleware": "^0.20.0",
    "koa": "^2.11.0",
    "koa-bodyparser": "^4.2.1",
    "koa-error": "^3.2.0",
    "koa-json": "^2.0.2",
    "koa-logger": "^3.2.1",
    "koa-router": "^7.4.0",
    "koa-service": "^1.0.1",
    "koa-static": "^5.0.0",
    "koa2-cors": "^2.0.6",
    "mysql": "^2.17.1",
    "mysql2": "^2.0.0",
    "nodemon": "^1.19.4",
    "sequelize": "^5.21.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.4.0",
    "@babel/preset-env": "^7.4.2",
    "@babel/register": "^7.4.0",
    "typescript": "^3.6.4",
    "mocha": "^5.2.0"
  }
}
  1. Paste directory structure
.
├── app.js
├── controller
│   └── userController.js
├── middleware
├── modle
│   └── user.js
├── mysql.js
├── routes
│   └── index.js
├── sequelize.js
└── service
    ├── index.js
    └── userService.js

5. The backstage code will not be pasted up one by one. Interested students can visit my git.
Walk you.

6. I almost forgot to talk about the configuration of database connection.

sequelize.js is used to connect to the database. This file is required for all the additions, deletions, changes and queries in this project.

Post it for everyone

const Sequlice = require('sequelize');

const sequelize = new Sequlice('Your database name', 'User name', 'Password', {
    host: 'data base host',
    dialect: 'mysql',
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000,
    },
});

module.exports = sequelize;

Note: when using sequelize, you don't need to go to mysql to create the data table manually. You only need a mapping file, which is the module / user.js file in my code. Specific usage reference How to use sequelize . You must be able to read it.

7. It may be necessary to add, delete, modify and query sql Foundation , you can familiarize yourself first, and then go sequelize official website Read the document, or you may be under pressure.

8. At this point, the background is basically over. In the next part, we will pack the front and rear projects into docker images and run them.

Posted by phorcon3 on Mon, 11 Nov 2019 14:28:22 -0800