本篇內(nèi)容介紹了“node如何封裝MySQL處理語(yǔ)句”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)專注于企業(yè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、漢南網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、商城開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為漢南等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
npm i mysql -S
npm i express -S
const mysql = require('mysql')function createConnection() {
const connection = mysql.createConnection({
host: '', //地址
user: '', //用戶名
password: '', //密碼
port: '', //端口
database: '' //數(shù)據(jù)庫(kù)名
});
return connection;}module.exports.createConnection = createConnection;
引入MySQL連接相關(guān)文件,進(jìn)行連接數(shù)據(jù)庫(kù)操作
const mysql = require('../mysql/mysql')let connection = null;connection = mysql.createConnection();/**
* 錯(cuò)誤消息
*/let bad_msg = {
code: 500,
msg: '內(nèi)部錯(cuò)誤!'}/**
* 成功消息
*/let success_msg = {
code: 200,
msg: '操作成功'}const connections = {
/**
* 查詢方法
* @param {*} table 表名
* @param {*} condition 條件
* @param {*} params 參數(shù)
* @param {*} search 查詢條件
* @returns
*/
find(table, condition, params, search = '*') {
return new Promise((resolve, reject) => {
let sql = `SELECT ${search} FROM ${table} WHERE ${condition}`
connection.query(sql, params, (err, result) => {
if (err) {
reject(bad_msg)
} else {
let _ = JSON.parse(JSON.stringify(success_msg))
_.data = result resolve(_)
}
})
})
},
/**
* 插入方法
* @param {*} table 表名
* @param {*} condition 條件
* @param {*} params 參數(shù)
* @returns
*/
insert(table, condition, params) {
return new Promise((resolve, reject) => {
const str = "?"
let _ = str.repeat((condition.split(',')).length)
let val = (Array.from(_)).toString()
let sql = `INSERT INTO ${table}(${condition}) VALUES(${val})`
connection.query(sql, params, (err, result) => {
if (err) {
reject(bad_msg)
} else {
resolve(success_msg)
}
})
})
},
/**
* 更新方法
* @param {*} table 表名
* @param {*} val 值
* @param {*} condition 條件
* @param {*} params 參數(shù)
* @returns
*/
update(table, val, condition, params) {
return new Promise((resolve, reject) => {
let sql = `UPDATE ${table} SET ${val} WHERE ${condition}`
connection.query(sql, params, (err, result) => {
if (err) {
reject(bad_msg)
} else {
resolve(success_msg)
}
})
})
},
/**
* 刪除方法
* @param {*} table 表名
* @param {*} condition 條件
* @param {*} params 參數(shù)
* @returns
*/
del(table, condition, params) {
return new Promise((resolve, reject) => {
let sql = `DELETE FROM ${table} WHERE ${condition}`
connection.query(sql, params, (err, result) => {
if (err) {
reject(bad_msg)
} else {
resolve(success_msg)
}
})
})
},}module.exports = connections
我們使用登錄注冊(cè)來(lái)進(jìn)行演示:
const express = require('express')const router = express.Router()const connections = require('../../static/connection')// token生成插件模塊const jwt = require('jsonwebtoken');// Token簽名var secret = ''const CreatId = require('../../static/ranId')router.post('/user/details', (req, res) => {
connections.find('user_table', `ID=?`,req.user.ID).then(resp => {
res.send(resp)
})})router.post('/api/login', (req, res) => {
connections.find('user_table', 'user=?', req.body.user).then(resp => {
let {data} = resp if (data.length !== 0) {
for (let i = 0; i < data.length; i++) {
// 郵箱或者密碼不正確的時(shí)候
if (req.body.user !== data[i].user || req.body.pwd !== data[i].pwd) {
res.send({
code: 202,
msg: '用戶名或密碼有誤!'
})
} else {
// 郵箱和密碼輸入正確
if (req.body.user === data[i].user && req.body.pwd === data[i].pwd) {
// 傳輸?shù)膖oken內(nèi)容
let token = jwt.sign({ ID: data[i].ID }, secret, { expiresIn: '72H' });
// 返回結(jié)果
res.send({
code: 200,
msg: '操作成功!',
token: 'Bearer ' + token,
})
}
}
}
} else {
res.send({
code: 400,
msg: '賬號(hào)不存在請(qǐng)注冊(cè)!'
})
}
}).catch(e => {
res.send(e)
})})router.post('/api/register', (req, res) => {
connections.find('user_table', 'user=?', req.body.user).then(resp => {
if (resp.data.length > 0) {
res.send({
code: 202,
msg: '該用戶已經(jīng)存在!'
})
} else {
let _ = req.body let id = CreatId(3) + CreatId(3)
connections.insert('user_table', 'ID,user,pwd,avatarUrl,location,RegisterTime,isAdmin,isDel', [id, _.user, _.pwd, '/static/userimg/user.webp', _.location, Date.now(), 0, 0]).then(resps => {
// 傳輸?shù)膖oken內(nèi)容
let token = jwt.sign({ ID: id }, secret, { expiresIn: '72H' });
// 返回結(jié)果
res.send({
code: 200,
msg: '操作成功!',
token: 'Bearer ' + token,
})
})
}
})})module.exports = router
“node如何封裝mysql處理語(yǔ)句”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!