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,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?