# KkdxBackend **Repository Path**: totodo/KkdxBackend ## Basic Information - **Project Name**: KkdxBackend - **Description**: KanKanDX - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-04-24 - **Last Updated**: 2024-06-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # KkdxBackend #### 介绍 KanKanDX 的服务后端 #### 软件架构 1. nodejs 20.12 2. express 4.18.3 3. cors 解决跨域调用 4. mysql2 mysql nodejs client驱动 #### 安装教程 ```shell npm i ``` #### 使用说明 ```shell npm run dev || node server.js ``` #### 开发说明 ##### 1 Mysql链接使用(CRUD) ###### 1.1 创建链接池 ```javascript const pool = mysql.createPool({ connectionLimit: 10, // 连接池中最大的连接数量 host: '43.142.34.49', // 数据库的地址 user: 'root', // 数据库的用户名 password: 'abcd1234', // 数据库的密码 database: 'advice_db' // 数据库的名称 }); ``` ###### 1.2创建记录(Create) ```javascript const query = "INSERT INTO your_table (column1, column2) VALUES (?, ?)"; const values = ['value1', 'value2']; pool.query(query, values, (error, results, fields) => { if (error) throw error; console.log(results); }); ``` ###### 1.3 读取 (Read) ```javascript const query = "SELECT * FROM your_table WHERE condition_column = ?"; const conditionValue = 'condition_value'; pool.query(query, conditionValue, (error, results, fields) => { if (error) throw error; console.log(results); }); ``` ###### 1.4 更新 (Update) ```javascript const query = "UPDATE your_table SET column = ? WHERE condition_column = ?"; const newValue = 'new_value'; const conditionValue = 'condition_value'; pool.query(query, [newValue, conditionValue], (error, results, fields) => { if (error) throw error; console.log(results); }); ``` ###### 1.5 删除记录(DELETE) ```javascript const query = "DELETE FROM your_table WHERE condition_column = ?"; const conditionValue = 'condition_value'; pool.query(query, conditionValue, (error, results, fields) => { if (error) throw error; console.log(results); }); ``` ###### 1.6 关闭数据库连接池中的链接(CLOSE) ```javascript pool.end(); ``` ##### 2.用ES中的现同步 ###### 2.1 Promise创建 ```javascript const promise = new Promise(function(resolve, reject) { // ... some code if (/* 异步操作成功 */){ resolve(value); } else { reject(error); } }); ``` ###### 2.1 Promise的使用 ``` javascript promise.then(function(value) { // success }, function(error) { // failure }); }) ``` ###### 2.2 Promise 捕获异常 ```javascript promise.then((val) => { console.log('fulfilled:', val) }).catch((err) => { console.log('rejected', err) }); // 等同于 promise.then((val) =>{ console.log('fulfilled:', val) }).then(null, (err) => { console.log("rejected:", err) }); //当 多个promise连续调用,每个都要检查异常时 promise.then( (value) => { // success //xxx }, (err)=>{ }).then(value=>{ }, (err) =>{ }).then(value=>{ }, (err) =>{ }) ``` ###### 2.3 Promise.all() 多个异步调用包装到一个操作里面 Promise.all()方法用于将多个 Promise 实例,包装成一个新的 Promise 实例。 ```javascript const p = Promise.all([p1, p2, p3]); ``` 上面代码中,Promise.all()方法接受一个数组作为参数,p1、p2、p3都是 Promise 实例,如果不是,就会先调用下面讲到的Promise.resolve方法,将参数转为 Promise 实例,再进一步处理。另外,Promise.all()方法的参数可以不是数组,但必须具有 Iterator 接口,且返回的每个成员都是 Promise 实例。 p的状态由p1、p2、p3决定,分成两种情况。 (1)只有p1、p2、p3的状态都变成fulfilled,p的状态才会变成fulfilled,此时p1、p2、p3的返回值组成一个数组,传递给p的回调函数。 (2)只要p1、p2、p3之中有一个被rejected,p的状态就变成rejected,此时第一个被reject的实例的返回值,会传递给p的回调函数。 ```javascript // 生成一个Promise对象的数组 const promises = [2, 3, 5, 7, 11, 13].map(function (id) { return getJSON('/post/' + id + ".json"); }); Promise.all(promises).then(function (posts) { // ... }).catch(function(reason){ // ... }); ``` #### 3.读取环境变量 ##### 3.1 安装依赖: 首先,你需要安装dotenv这个包,它可以帮助加载.env文件中的环境变量。可以通过运行以下命令来安装它: ``` npm install dotenv ``` ##### 3.2 创建.env文件 ```ini DB_SERVER=43.142.34.49 DB_USER=root DB_PASSWORD=yourpasswrd ``` #### 3.3 配置Express应用得变量: 在你的Express应用启动文件中(通常是app.js或index.js),使用dotenv来加载.env文件中的环境变量。 ```javascript require('dotenv').config(); ``` ```javascript require('dotenv').config(); const dbserver = process.env.DB_SERVER const dbUser = process.env.DB_USER; const dbPass = process.env.DB_PASSWORD; const dbname= process.env.DB_DATABASE const pool = mysql.createPool({ connectionLimit: 10, // 连接池中最大的连接数量 host: dbserver, // 数据库的地址 user:dbUser, // 数据库的用户名 password: dbPass, // 数据库的密码 database: dbname // 数据库的名称 }); ``` #### 4 使用Router模块化API ##### 4.1 使用独立文件例如user.js处理相关路由API // 用户相关的路由 ```javascript // users.js const express = require('express'); const router = express.Router(); router.get('/user/getUsers', (req, res) => { // 路由相关代码 }) module.exports = router; //导出模块 ``` ##### 4.2 在主文件中引入路由 ```javascript const app = express(); const usersRouter = require('./users'); // 引入users.js中的路由器 // 挂载路由器到特定的路径 //定义和使用Router 来模块化 API app.use(usersRouter) ```