This post will be explaining my discoveries and experience learning how to use node.js and MySQL together.
To give a little background: I have had basic experience with MySQL using it in a couple projects with netbeans, and have used SQLPLUS for about a year, which is quite similar to MySQL. As for node.js, this will be my first time exploring the tool.
Before beginning any tutorials for node.js, my understanding is that it is a tool that allows JavaScript to be used as the programming language in the backend. So, this means that JavaScript could be used for both the backend and frontend resulting in the entire project being done in one language.
To get an understanding of how node works I began by following the introduction to node.js documentation located at: https://nodejs.dev/en/learn/
Following the tutorial, I wrote the code above which was the first part of the tutorial, that creates a basic web server.
After entering the command into the terminal:
node app.js
(app.js is what I named the file in which the code is in)
You can then go to the address that was set as the hostname and port in a browser and view the page:
Now that I had node.js installed and was able to get a basic web server running, I wanted to broaden my understanding of how the framework does things. For this I went to w3schools series of tutorials for node.js to figure out how things work. This is the point where I began to understand the purpose and special features that make node so good. To begin, it is free and open sourced, and can run on Windows Linux and Mac OS. Its big idea is that it uses JavaScript on the server-side and it is asynchronous, so when a task is given to the server it is able to initiate that task and wait for responses while also dealing with other tasks. Continuing my research, I began to understand how various things in node.js work. The first, and arguably the most important being the http.createServer() method, which turns your computer into a server (this was used in the initial introduction tutorial shown in the first screenshot) . Upon further reading, I also learned about modules, which are just like libraries. For example, in the first tutorial I did, there is a module used in line one:
const http = require(‘http’);
Not only can the built-in modules be used, but also you can create your own. To do this the user needs to create a new .js file, and inside create whatever functionality you would like the module to do. Then, in your node.js project file use the command shown above while replacing the ‘http’ with the name and path of your module file.
Node.js basically revolves around the use of different modules in order to create the desired project. Some of these modules include: nodemailer(email), fs(filesystem), http, events(used to handle events). But like I said earlier you can create your own if there is not a built-in module that can be of use in your situation.
As for my next course of learning, I needed to find out how node works with a database like MySQL. To learn more, I decided to use the documentation that is on w3schools which had a section specifically for Node and MySQL: https://www.w3schools.com/nodejs/nodejs_mysql.asp. This is my main source of reference as it has lots of very useful chapters that teach how to do various things such as: Creating a database and tables, insertion and deletion, as well as queries among other things.
So, I started following the tutorials, beginning with creating a database. The first step I had to take was to download the mysql package which included the drivers and modules to use MySQL with Node.js. This was done using a NPM command in the terminal, which is a package manager for node(Node Package Manager).
Installing the package to the project is as easy as typing in the command:
Now that I had the required package, I could begin using mysql with node.
The first step was to create a connection to the database to ensure node is communicating with MySQL properly. It ended up being quite simple as you just need to use the mysql.createConnection() method and inside the brackets, specify the parameters for the host and the user name and password for your MySQL. Once that is done, the only other function needed is a connect function which runs the create connection. As soon as I ensured the connection was working, it was time to start creating the database.
For this, you do a query which is accessed by using the .query() method and writing your query in the first parameter. For example, the query to create a databes would be:
CREATE DATABASE databasename
Now if we move onto table creation this kind of changes the way you may look at the database creation. It may be better to already have a database created rather than create the database using node, because when you want to start adding tables and using those tables, you need to specify the database in the mysql.createConnection() method by adding the line:
database: “databasename”
So already having a database initialized before using node may be the best approach. Anyways, continuing with table creation, once you specify what database you are using, you just do another .query method call inside the connect function (just like the database creation) then type in your CREATE TABLE query into the first parameter.
The steps used for the table creation are the same steps that will be used for pretty much every other query, you just use the .query method and type in your query.
So far this has been a very interesting study as well as relieving in a way because I did not expect node.js with MySQL to be formatted in such an easy to use and straightforward way. As for the future of my study on this topic, now that I understand how to perform a query and also have a better understanding of how node.js works, I want to create some sort of application that will use these tools to put my knowledge to the test. What I am thinking currently is that it will probably be some sort of application that allows a user to store information in the database, and manipulate that information. As for now, that will conclude this post, I look forward to learning more and will update my experience on my next post. Cheers!



Top comments (0)