Js code connecting to the database:
dbase.js
1 // derived tidied mysql module 2 // query that is passed to the SQL statement, written as a function of the parameters of the callback function . 3 the let mSQL = (SQL, the params, the callback) => { . 4 const = mysql the require ( 'mysql ' ); . 5 const = Conn mysql.createConnection ({ . 6 Host:' localhost ' , . 7 Port: 3306 , . 8 User:' the root ' , . 9 password:' the root ' , 10 Database:' Heima ' . 11 }); 12 is / / connection mysql server 13 is conn.connect (); 14 //Prior to End, implement the query operation 15 conn.query (SQL, the params, the callback); 16 // Close mysql server . 17 conn.end (); 18 is } . 19 // export module (derived when an object or export, or derive function, where function is derived mSQL 20 is module.exports = mSQL;
Other js file database operations need to be loaded dbase.js modules:
1 // using a lookup table operation mysql 2 const the require DB = ( 'User ./' ); . 3 DB ( 'SELECT ID, name, Age from Student', null , (ERR, Result) => { . 4 IF (ERR ) { . 5 the console.log (ERR); . 6 } . 7 the console.log (Result); . 8 })