This article is based on a simple combination of development on Mac, node && MySQL.
I. Building the Environment
- Download the relevant version of your computer to the official website. Then start MySQL
- When using NPM-V to detect whether the computer installed Nodejs, if not on the node website to download the appropriate version to install, is the version installed on the machine
- Build your project with NPM, install Express and MySQL, and execute npm install express MySQL--save in the created directory
- The express here is temporarily unused and can be used without installation
II. Structure of the project
This project is a simple combination of node and MySQL, so the structure is relatively simple, as follows
"Digression, I use the editor is VSC"
Iii. combined use of Node&mysql
- Execute the relevant code into the database and create the database, I created the test database as follows
- Enter the following code in the project that you created:
Here is the introduction of the database var mysql = require (' mysql ');//Create link var connection = mysql.createconnection ({ host: ' localhost ', User: ' root ', password: ' YES ', database : ' Reng '}//Start link connection.connect ();//Operate the database Connection.query (' SELECT * from students ', function (err,rows,fields) { if (err) { console.log (err); } if (rows) {for (var i =0; i<rows.length;i++) { console.log (rows[i].name+ '-to ' +rows[i].age);}} ) Disconnect link connection.end ();
Iv. observation Results
Execute node app.js in the corresponding folder, and you will see the following results
"Results are consistent with expectations"
"Original, quote please specify the source, thank you"
Use node && mysql together