Browse Source

热部署

CzRger 1 year ago
parent
commit
3b72a85abe
3 changed files with 56 additions and 7 deletions
  1. 5 6
      app.js
  2. 47 0
      mysql.js
  3. 4 1
      package.json

+ 5 - 6
app.js

@@ -1,13 +1,12 @@
-// 导入 express
-const express = require('express')
+import express from 'express'
+import cors from 'cors'
+import bodyParser from 'body-parser'
+import multiparty from 'connect-multiparty'
 // 创建 app
 const app = express()
 // 设置跨域访问
-const cors = require('cors')
 app.use(cors())
 // 处理POST参数
-const bodyParser = require('body-parser')
-const multiparty = require('connect-multiparty')
 // 处理 x-www-form-urlencoded
 app.use(bodyParser.urlencoded({
     extended:true
@@ -18,7 +17,7 @@ app.use(bodyParser.json())
 app.use(multiparty())
 // 测试接口能否正常调用
 app.get('/info', (req, res) => {
-    res.send("Hello shaoyahu !")
+    res.send("Hello s !")
 })
 // 启动
 app.listen(3000, () => {

+ 47 - 0
mysql.js

@@ -0,0 +1,47 @@
+import * as mysql from 'mysql'
+const db_config = {
+    host: '8.140.240.182',
+    user: 'root',
+    password: 'Taiji@2023#data',
+    port: "18080",
+    database: 'ax_seat_show'
+}
+// 进行数据库交互
+const conMysql = (sql) => {
+    let connect = mysql.createConnection(db_config)
+    // 开始链接数据库
+    connect.connect(function (err) {
+        if (err) {
+            console.log(`mysql连接失败: ${err}!`)
+        } else {
+            console.log('mysql连接成功!')
+        }
+    })
+    // 返回一个Promise承诺对象
+    return new Promise((resolve, reject) => {
+        connect.query(sql, (err, result) => {
+            if (err) {
+                reject(err)
+            } else {
+                // 此处需要将返回数据转为JSON再转回来,否则原数据不为任何数据类型
+                let res = JSON.parse(JSON.stringify(result))
+                closeMysql(connect)
+                resolve(res)
+            }
+        });
+    })
+}
+// 查询成功后关闭mysql
+const closeMysql = (connect) => {
+    connect.end((err) => {
+        if (err) {
+            console.log(`mysql关闭失败:${err}!`)
+        } else {
+            console.log('mysql关闭成功!')
+        }
+    })
+}
+// 导出方法
+export {
+    conMysql
+}

+ 4 - 1
package.json

@@ -1,6 +1,7 @@
 {
   "name": "ax-node",
   "version": "1.0.0",
+  "type": "module",
   "description": "",
   "main": "index.js",
   "scripts": {
@@ -13,6 +14,8 @@
     "body-parser": "^1.20.2",
     "connect-multiparty": "^2.2.0",
     "cors": "^2.8.5",
-    "express": "^4.18.2"
+    "express": "^4.18.2",
+    "mysql": "^2.18.1",
+    "nodemon": "^3.0.3"
   }
 }