Introduction :

When we're writing background programs , Always write some data to the database , So reasonable database function call is very necessary , This article is written for use node Connect to database , And it is encapsulated into functions .

<>1, create a file conf, Create in it db.js, The account used to store the database also has a password .
// Select the database according to the development or production mode const env = process.env.NODE_ENV let MYSQL_CONF let
REDIS_CONF if (env === 'dev') { MYSQL_CONF = { host: 'localhost', user: 'root',
password: 'xiaomizhou123', port: '3306', database: 'mydb' } //redis_conf
REDIS_CONF = { port: 6379, host: '127.0.0.1' } } if (env === 'production') {
//mysql MYSQL_CONF = { host: '172.**.**.**', user: 'root', password:
'xiaomizhou123', port: '3306', database: 'mydb' } //redis_conf REDIS_CONF = {
port: 6379, host: '172.**.**.**' } } module.exports = { MYSQL_CONF, REDIS_CONF }
<>2, establish db folder , Create mysql.js, It is used to write database unified execution function .
const mysql = require('mysql') const { MYSQL_CONF } = require('../conf/db') //
Create linked objects const con = mysql.createConnection(MYSQL_CONF) // Start link con.connect() //
Unified implementation sql Function of function exec(sql) { const promise = new Promise((resolve, reject)
=> { con.query(sql, (err, result) => { if (err) { reject(err) return } resolve(
result) }) }) return promise } module.exports = { exec, escape: mysql.escape }
<>3, establish controller folder , Create user.js, to write sql Splicing function , Used to generate complete sql sentence .
const { exec, escape } = require('../db/mysql') const { genPassword } = require
('../utils/cryp') // land const login = async (username, password) => { username =
escape(username) // Generate encrypted password password = genPassword(password) password = escape(
password) console.log(password) const sql = ` select username, realname, avatar
from users where username=${username} and passworded=${password} ` const rows =
await exec(sql) return rows[0] || {} } module.exports = { login }
supplement :

Wechat search 【web Small hall 】, Reply to full stack blog project , You can get the source code of the project and the subsequent practical article tutorial
. Use the simplest and simple language every day , Imperceptibly improve your computer basic knowledge and front-end technology . Millet Congee , A dedicated web Full Stack Developer , See you next time !

Technology
©2019-2020 Toolsou All rights reserved,
How to prevent Mac put to sleep ? Tell you 4 This is an effective way be based on C# Students' grades / mis solve element-ui The drop-down box of has a value but cannot be selected Student information management system source code -C language VR,AR and MR The difference between these technologies 【 Data structure and algorithm 6】 Stack implementation of comprehensive calculator utilize python Yes monkey Automatic log analysis Laya Wechat games sub package loading Alibaba enterprise email smtp set up ( practice )【Java Details of knowledge points 3】 Serializable and Deserialize