Node.js Tutorial | MySQL Database Connection in Express.js Application

In Web Development


Node.js Tutorial | MySQL Database Connection in Express.js Application - read the full article about Node Js update, Web Development and from Webslesson on Qualified.One
alt
Webslesson
Youtube Blogger
alt

hello friends in this node.js video tutorial we will learn how to make mysql database connection in the node express application and in one of the previous tutorial we have already show you how to make mysql database connection in node application [Music] but in that tutorial we have not used node express application so here we have again make video tutorial on mysql database connection topic this is because here we have used node express framework for make crud application in node js so in this part we will learn how to connect [Music] node express js application with mysql database now lets start discussing this topic so this is our testing database and we will connect node express applications with this database so first we have go to command prompt and here we want to install node express mysql module [Music] so here we have to run following command [Music] this command will download and install node express mysql module so here we can see that node express mysql module has been installed in our node express application now we have go to text editor and here we can see directory structure of node express application and in the node module folder here we can see that node express mysql module has been downloaded under this directory now we want to create database connection file so here we have create new javascript file in the root folder with file name like database.js here we will use this file for make mysql database connection and under this file we have to write constant mysql variable is equal to require function and under this function we have to write mysql so by using this code it will include mysql module under this file after this we want to define configuration for creating mysql database connection so here we have to write constant connection variable is equal to mysql.createconnection function so this function will make mysql database connection and under this function we have to define mysql database configuration in the json format so first we have to define host for mysql database connection so in first key we have to write host and in value we have to write localhost [Music] next we want to define database name from which we want to connect out node application so for this in the key we have to write database and in the value we have to write testing and after this we want to define user name of the mysql database so for this in the key we have to write user and in the value we have to write root lastly in the mysql database configuration we want to define password of the mysql database so for this here we have to write password and in the value we have to write blank so after define this configuration it will try to make mysql database connection now we want to execute mysql database connection so for this we have to write connection dot connect function so this function has been used for create a mysql database connection and under this function we have to write one callback function with error as parameter [Music] under this function we have to write its statement and under condition we have to write error variable so this condition will check if there is any error occurs then it will execute if block of code and under this block we have to write throw error so it will display database connection error but suppose there is no any error occurs then it will execute else block of code and under this block we have to write console.log function [Music] and under this function we have to write mysql database is connected successfully so here our mysql database connection code is ready now we need to use this file and the roots folder sample data dot js file so for this here we have to write module dot exports is equal to connection variable so after writing this line of code so this pile of code can be used in another file also so we can easily make mysql database connection in any file by using this file code now we have go to roots folder and here we have to open sampledata.js file and under this file we have to write database variable is equal to require function [Music] and under this function we have to write double dot slash database so by using this code we can include database file under this sample data file and under this file it will do database operation so here our code is ready now we have saved this file and go to command prompt and run following command [Music] so this command will start node.js server and here we can see that mysql database connection message on the screen so here our node express application has been connected with mysql database so in this part we have learnt how to connect node express application with mysql database and in the next part we will fetch database from mysql database and display on the web page in html format so we will be meet in the next part

Webslesson: Node.js Tutorial | MySQL Database Connection in Express.js Application - Web Development