We create a mysql.js (like the great gods, call this a module and then expose an interface) to connect to the database
var connction ={};
Connction.mysql = {
Host: "localhost",//This is the address of the database
User: "Root",//username required
Password: "root",//user password, if you do not have a password, the direct double quotation mark is
Database: "MySQL"//DB name
}//OK, so we can connect to the database.
Of course, it's impossible.
Module.exports = connction; Exposing this interface with Module.exports,
We want to install the MySQL module
Installing MySQL--save-dev with cnpm install
Create a App.js file
var express = require ("Express");
var $mysql = require ("MySQL");
var sql = require ("./mysql"); In this sentence, the MySQL template that introduced the current directory MySQL is the mysql.js we created above
var $sql = $mysql. CreateConnection (sql.mysql)//Create a connection MySQL is the template that we exposed above the file method
$sql. Connect ()//Use this phrase to really connect
node. JS connects to MySQL database with Express