node. js Database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.