node简易服务器完成

This commit is contained in:
aixianling
2022-03-29 15:04:07 +08:00
parent 0905e9263c
commit d48be5b91f
8 changed files with 120 additions and 13 deletions

24
utils/dbUitls.js Normal file
View File

@@ -0,0 +1,24 @@
const mysql = require("mysql");
const dbConfig = require("../config/db");
module.exports = {
pool: null,
init: () => {
this.pool = mysql.createPool(dbConfig)
},
query: sql => new Promise(resolve => {
this.pool?.getConnection((err, conn) => {
if (err) {
console.log(err)
} else {
conn.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
conn.release()
resolve(result)
}
})
}
})
}),
}

37
utils/fsUtils.js Normal file
View File

@@ -0,0 +1,37 @@
const fs = require("fs")
const path = require("path")
const promisify = fn => {
return function () {
let args = arguments;
return new Promise(function (resolve, reject) {
[].push.call(args, function (err, result) {
if (err) {
console.log(err)
reject(err);
} else {
resolve(result);
}
});
fn.apply(null, args);
});
}
}
const readdir = promisify(fs.readdir)
const stat = promisify(fs.stat)
const findFile = (dir = '.', cb) => {
return readdir(dir).then(apps => {
return Promise.all(apps.map(e => {
let cPath = path.join(dir, e)
return stat(cPath).then(state => {
if (state.isDirectory()) {
return findFile(cPath, cb)
} else if (state.isFile()) {
cb && cb(cPath)
}
})
}))
})
}
module.exports = {readdir, stat, findFile}