Connecting nodejs app to Mysql

This article explains how to connect Node.js to Mysql. Mysql is most popular and widely used open source Database and Node.js is a non-blocking and Asynchronous, That enables us to create very fast server-side applications.Combining these two would make your web development very useful needless to say. So let’s have a look how we can Connect NodeJs to Mysql.

To demonstrate the connection between Node.js and Mysql am taking the example of Ajax Auto Search Box. Basically, we’ll have a textbox and whatever you will write into it, you will get the response from the server based on the text written inside the textbox.Since those results will be stored in the MySQL database table, SO we will be using some kind of Mysql query to  fetch it, we’ll go through down the road. Speaking of down the road, I forgot to mention Here I will be using the AngularJs for the front end.You can use any front-end framework of your choice.




 

     Download

     Demo

 

Also, Read Realtime chatting application using Node.js Socket.io AngularJS and Mysql.

 

1. Understanding the project structure

1. As I said earlier, here we will create an application, which will imitate the functionality of Ajax Auto Search, in other words, autocomplete functionality.Here we will give a very little bit of styling to our web application just to make it look presentable.And as I said, we will just AngularJs to make a server side call in order to fetch the results. So below is our project folder structure.

2. Inside the client folder, we will write down the MARKUP, CSS, and all the client-side Javascript scripts, including CSS and Javascript libraries.

http://www.codershood.info/wp-content/uploads/2015/12/Connecting nodejs to Mysql

 

3. Let’s start off by creating a new Nodejs project by using ng init command.This command will create a new package.json file. Below is my package.json file for this application.

package.json:

2. Creating a NodeJs Server

1. Create a server.js in the root of the project, which will be our entry point for the project.Here we will define our route and we will start the server.

=> The appConfig() method will set the application configuration.

=> In includeRoutes() method we will execute the application route.

=> The appExecute() method will call the appConfig() as well as appExecute() and it will run the nodejs server.

server.js:

3. Connect NodeJs to Mysql

1. Now we have written the code to start the Nodejs server, let’s write the code to connect Nodejs to MySql. Create helper.js file inside the root of the project folder.In this file, we will write nodejs Connectivity code and we will write a method to execute a query to fetch results from the database.

=> The constructor() method will connect the Nodejs to mysql.

=> In getSuggestion() method , we  have written the MySql LIKE query to fetch the result from Table.

helper.js:

4. Creating the FrontEnd

1. Now that we have completed all the server side work let’s start the development of front-end work. Create an index.html inside the /client folder and write done the below markup.

=> In the below markup we will render the results using ng-repeat.

index.html:

 

2. Now let’s write the write the AngularJs code in order fetch the result from the server by making an HTTP call. So create a script.js inside the /js folder under the client folder.

=> First, we will create service which will make an HTTP call and returns the HTTP response back to the caller.

=> Here we will use $watch() function, in order to make a call to appService, Once the response received from the server we will render the response using ng-repeat as I mentioned earlier.

script.js:

 

So this was a small demonstration of Nodejs and Mysql database connectivity. Please be sure to comment down below if you have any question or suggestion in order to add something in this tutorial, I’ll happy to know.

Till then Happy coding 😀
 

  • ARVIND A

    I am fresher and interested in Angularjs and nodejs.This article is very nice
    I tried to run this application in visual studio but i could not run this.
    please give me the steps to run this application.

    • Hello ARVIND, Thanks for stopping by. To run nodejs app first, make sure u have installed NODEJS & Mysql (Xampp, Wamp or Lamp will be fine).

      1. open the directory where u have your nodejs application, copy the path now go to the CMD.

      2.Enter into that directory by writing cd command.

      3. Now write node server.js and your application should run properly.

      hope this helps.

  • Loy houngnakhone

    why I can not download this file? any body help me please.
    I can download about 50% and after that can not continues to download

    • Hello Loy, Sorry for the trouble but that’s very rare, However am sending you the source code.

      • shahid ahmad

        Hi Shashank,
        Can you please send me the source code because its not downloadable.It’ll be apriciated.Thanks. 786sag@gmail.com

  • dummy mail

    hi shashank.. Im a fresher and interested to learn nodejs and angularjs
    Can u pls tell me how to run this?

  • Ramu

    why I can not download this file? any body help me please.
    I can download about 50% and after that can not continues to download.

  • Ramu

    plz send full code this is my gmail id ::: dramu402@gmail.com