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

8
config/db.js Normal file
View File

@@ -0,0 +1,8 @@
module.exports = {
host: "192.168.1.87",
user: "root",
port: 3306,
password: "Cwy@2019",
database: "dvcp_v2_dev",
multipleStatements: true
}

11
demo.js
View File

@@ -1,11 +0,0 @@
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})

15
index.js Normal file
View File

@@ -0,0 +1,15 @@
const express = require('express')
const db = require('./utils/dbUitls')
const rest = require('./rest')
const app = express()
const port = 12525
app.listen(port, () => {
console.log('启动数据库连接池...')
db.init()
console.log('启动接口...')
rest.init(app).then(()=>{
console.log(`serve is listening on ${port}`)
})
})

View File

@@ -4,7 +4,7 @@
"description": "node服务端",
"main": "index.js",
"scripts": {
"test": "node demo.js"
"dev": "node index.js"
},
"repository": {
"type": "git",
@@ -14,8 +14,13 @@
"node",
"js"
],
"engines": {
"node": "16.13.1"
},
"author": "kubbo",
"dependencies": {
"express": "^4.17.3"
"express": "^4.17.3",
"helmet": "^5.0.2",
"mysql": "^2.18.1"
}
}

16
rest/index.js Normal file
View File

@@ -0,0 +1,16 @@
const {findFile} = require("../utils/fsUtils");
module.exports = {
init: ins => {
return findFile('./rest', file => {
if (!/index\.js/.test(file)) {
let rest = require(file.replace(/rest/, '.'))
console.log(`初始化接口...${rest.action}`)
if (rest.method == "post") {
ins.post(rest.action, (req, res) => rest.execute(req, res))
}
}
}).then(() => {
console.log("接口初始化完毕")
})
}
}

13
rest/sysuser/sysuser.js Normal file
View File

@@ -0,0 +1,13 @@
const dbUtils = require("../../utils/dbUitls");
module.exports = {
action: "/sys/user",
method: "post",
execute: (request, response) => {
dbUtils.query(`select * from sys_user`).then(res => {
response.send({
code: 0,
data: res
})
})
}
}

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}