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,
Non preemptive static priority scheduling algorithm for operating system (C language )Go Language learning notes (GUI programming )XCTF Attack and defense world web Advanced practice _ 2_lottery What's the difference between computer major and training background ?python realization vlookup_ Dry goods I : Why python It's inside vlookup Bubble sort primary springboot2 Separation of front and rear platforms ,token Put in header Pit for verification Python Case conversion of letters ( Two methods )javascript event ( Detailed explanation of zero basis )Unity2019 UIElement note ( ten ) Simple exercise 2