1. Environmental preparedness
To manually add a database dependency:
Added in Package.json's dependencies, "MySQL": "Latest",
{ "name": "News", "version": "0.0.0", "private": True, "scripts": { "start": "Node./bin/www" }, "dependencies": { "Body-parser": "~1.13.2", " cookie-parser": "~1.3.5", "Debug": "~2.2.0", " Express": "~4.13.1", "Jade": "~1.11.0", "Morgan": "~1.6.1", "Serve-favicon": "~2.3.0", " MySQL": "Latest" }}
Use the command to install MySQL and add dependencies:
NPM Install Mysql-save
2. Official Examples:
Copy code//connection database var MySQL = require (' mysql '); var connection = Mysql.createconnection ({ host: ' localhost ', User: ' Root ', password: ' open00 ', database: ' Test '}); Connection.connect ();//Query Connection.query (' select * from ') MyTable ', function (err, rows, fields) { if (err) throw err; Console.log (' Query result: ', rows);}); /close connection connection.end ();
Run node
Nodejs using MySQL