1. Install node

node -v

Express and KOA2 generators create projects

npm install express-generator -g

express project

npm install koa-generator -g

koa2 project

The two projects are jade and PUG’s template engine. I don’t want to learn their syntax, so I change to EJS’s template engine:

npm install ejs

app.set(‘view engine’, ‘ejs’); // express

or

app.use(views(__dirname + ‘/views’, { extension: ‘ejs’ })) // koa2

3. Connect to the database

docker pull mysql

docker run –name mysql-docker -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql

docker exec -it mysql-docker mysql -uroot -p

or

docker exec -it mysql-docker bash

mysql -uroot -p

Mysql8.0 and mysql5.0 have different encryption methods: (mysQL8.0 :caching_sha_password, mysQL5.0 :mysql_native_password). The local Workbench could connect, but the Navicat connection failed.

show databases;

use mysql;

select host,user,plugin from user;

alter user ‘root’@’%’ identified with mysql_native_password by ‘123456’;

flush privileges;

select host,user,plugin from user;

exit;

// routes/database.js
/ / Mysql connection
var mysql = require('mysql');
// Database connection configuration
var pool = mysql.createPool({
  host: '119.45.12.238'.// Address of the database
  port: '3307'.user: 'root'.// Database user name
  password: '84529563f'.// Database password
  database: 'wenlx'   // Database name
})
console.log(pool._allConnections.length) / / 0
// Add, delete, change and query the base of the database operation
function query(sql, callback) {
  pool.getConnection((err, connection) = > {
    console.log(pool._allConnections.length) / / 1
    connection.query(sql, (err, rows) = > {
      callback(err, rows)
      connection.release()
    })
  })
  console.log(pool._allConnections.length) / / 1
}

exports.query = query
Copy the code
// routes/user.js
var express = require('express');
var router = express.Router();

// Import the database configuration file
const db = require('./database')

// Get the user table data from the database
router.get('/'.(err, res) = > {
  const sql = 'SELECT * FROM user';
  db.query(sql, (err, result) = > {
      if(err){
          return;
      }
      // res: API data transfer
      // result: The returned data needs to be converted to JSON format
      res.json({
        data: result,
        total: 4
      }); 
  }); 
}) 
module.exports = router;
Copy the code

Local test successful:

Deployment line:

// Dockerfile
FROM node:lts-alpine312. as build

RUN npm config set registry https://registry.npm.taobao.org

ADD ./express /home/wenlx/dist

WORKDIR /home/wenlx/dist

RUN npm install

EXPOSE 3000

CMD ["node"."./bin/www"]

# docker build -t expresstest .
# docker run -d -p 8888:3000 expresstest
Copy the code
// docker-compose.yml
version: "3.7"

services: 
  html:
    container_name: mysql_docker
    image: express:test
    build:
      dockerfile: Dockerfile
      context:.ports:
      - 8808:3000

# docker-compose build --no-cache && docker-compose up -d
Copy the code

4, local postman test

5. Solve cross-domain problems

Create a new Web project and use AXIos to request:

Solution 1: Web proxy

Solution 2: The server allows cross-domain access

NPM install cors-s or NPM install koA2-cors-s

const cors = require(‘cors’)

app.use(cors())