Connect to the database through the node. JS language and indicate the location of the database connection through the Express framework.
I. How to do the node. JS Connection Database under Express framework
Write Setting.js and connet.js in the same directory, respectively, responsible for database setup and connection
Setting.js
Module.exports = {host:' ... ', User:' root ', password:' 123456 ', Port:3306, Database:' DB name '};
Connect.js
var settings = require ('./settings ' var mysql = require (' mysql ' Client.threadid);}) Client.on ( ' error ', function () {}); Module.exports = client;
Loading of database modules under Express framework
Let db = require ('./database/querydb.js '); App.use (Express.static ('./public ')); App.use (express.static ('./database ')); App.use (Express.static ('./build '))
Second, simple node. JS database Connection and operation
Database connection:
var mysql = require (' mysql ' ' root ' ' 123456 ' ' 3306 ' function if ' Error connecting: ' + Client.threadid);})
View Code
Select operation
var sql = ' SELECT * from websites '; // check connection.query (SQL,function (err, result) {if(err) {Console.log (' [ SELECT ERROR]-', err.message); return ;} Console.log ('--------------------------SELECT----------------------------'); Console.log (result); Console.log ('------------------------------------------------------------\ n '); }); Connection.end ();
View Code
Delete operation
var delsql = ' DELETE from websites where id=6 '; // Delete connection.query (delsql,function (err, result) {if(err) {Console.log ( ' [DELETE ERROR]-', err.message); return ;} Console.log ('--------------------------DELETE----------------------------'); Console.log (' DELETE affectedrows ', result.affectedrows); Console.log ('--------------------------------------------- --------------------\ n '); }); Connection.end ();
View Code
Insert operation
varMySQL = require (' MySQL ');varConnection =mysql.createconnection ({host:' localhost ', User:' Root ', Password:' 123456 ', Port:' 3306 ', Database:' Test ', }); Connection.connect ();varAddsql = ' INSERT into websites (id,name,url,alexa,country) VALUES (0,?,?,?,?) ';varAddsqlparams = [' Rookie tool ', ' https://c.runoob.com ', ' 23453 ', ' CN '];//IncreaseConnection.query (Addsql,addsqlparams,function(err, result) {if(Err) {Console.log (' [INSERT ERROR]-', err.message);return;} Console.log ('--------------------------INSERT----------------------------');//console.log (' INSERT ID: ', result.insertid);Console.log (' INSERT ID: '), result); Console.log ('-----------------------------------------------------------------\ n '); }); Connection.end ();
View Code
Update operation
varModsql = ' UPDATE websites SET name =?, url =? WHERE Id =? ';varModsqlparams = [' Rookie Mobile Station ', ' https://m.runoob.com ', 6];//ChangeConnection.query (Modsql,modsqlparams,function(err, result) {if(Err) {Console.log (' [UPDATE ERROR]-', err.message);return;} Console.log ('--------------------------UPDATE----------------------------'); Console.log (' UPDATE affectedrows ', result.affectedrows); Console.log ('-----------------------------------------------------------------\ n ');}); Connection.end ();
View Code
node. js Database