Mysql Database Configuration and connection on the node. js platform, node. jsmysql
First download the mysql module package
Npm install mysql -- save-dev
Create a module for the database and put it in a file.
Var mysql = require ("mysql"); exports. connect = function (SQL, param, callback) {var db = mysql. createConnection ({host: "localhost", // Database URL port: "3306", // database port, default: 3306 user: "root", password: "root ", database: "databaseName"}); db. connect (); db. query (SQL, param, callback); db. end ();};
Other files can be used through the connect interface.
For example, http requests are intercepted in app. js.
Var db = require (". /routes/db. js "); app. get ("/index.html", function (req, res) {db. connect ("select * from t_user", function (error, data) {console. log (data) ;}); res. send ("
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!