- You can’t run Nodejs on XAMPP server. Because Nodejs was built top on Google V8 engine. Nodejs Organization has their own server Installation file on their website.
- Node.js and MySQL are some of the necessary binding needed for any web application. MySQL is one of the most popular open-source databases in the world and efficient as well. Almost every popular programming language like Java and PHP provides drivers to access and perform operations with MySQL. In this Node js and MySQL tutorial, ….
- Node JS installed on your PC. Basic understanding of Node JS and Express JS. Knowledge of SQL, you should know and understand how to query a database. Phpmyadmin installed on your PC. I recommend installing xampp as it already contains phpmyadmin in it. Understand how to use templating engines - we are going to be using ejs in this tutorial).
The most recent Stable (GA) release of MariaDB Connector/Node.js is:
MariaDB Connector/Node.js 2.5.3
MariaDB Connector/Node.js is used to connect applications developed on Node.js toMariaDB and MySQL databases. The library is LGPLlicensed.
|Date||Release||Status||Min. Node.js Compat.||Release Notes||Changelog|
|24 Feb 2021||2.5.3||Stable (GA)||Node 10+||Release Notes||Changelog|
|4 Dec 2020||2.5.2||Stable (GA)||Node 10+||Release Notes||Changelog|
|26 Oct 2020||2.5.1||Stable (GA)||Node 10+||Release Notes||Changelog|
- About MariaDB Connector/Node.js
About MariaDB Connector/Node.js
Obtaining the Driver
The required files can be downloaded from:https://mariadb.com/downloads/connector
The source code is available on GitHub:https://github.com/MariaDB/mariadb-connector-nodejs
Installing the Driver
The driver can be installed using npm:
Choosing a Version
Driver versions are compatible with all MariaDB servers and MySQL 5.x (>= 5.5.3). Tested with MariaDB server versions 5.5, 10.0, 10.1, 10.2, 10.3 and 10.4 with Node.js 6 to 12 (see travis results for unix or appveyor results on windows).
MariaDB Connector/Node.js requires Node.js 6 or above, since it is based on Promise.
GNU Lesser General Public License as published by the Free Software Foundation;either version 2.1 of the License, or (at your option) any later version.
Using the Driver
The MariaDB Connector can use different APIs on the back-end: Promise and Callback API. The default API is Promise. The callback API is provided for compatibility with the mysql and mysql2 APIs.
Hello world!. In this tutorial, we are going to build a simple CRUD application using Node JS and MySQL.
What we will build?
The image above shows the app. It is an application that lets you add players to a database and also display their details from the database. You can also delete and edit player details.
Before you join this tutorial it is assumed that you meet the requirements listed below:
- Node JS installed on your PC.
- Basic understanding of Node JS and Express JS.
- Knowledge of SQL, you should know and understand how to query a database.
- phpmyadmin installed on your PC. I recommend installing xampp as it already contains phpmyadmin in it.
- Understand how to use templating engines -- we are going to be using ejs in this tutorial).
- A text editor or IDE of your choice.
Node Js Xampp Mysql Example
This is how the project will be structured.
Using Mysql With Node Js
Creating the directory for the project
Open the command prompt in a suitable directory and type the following command:
then change to the directory by typing the following command
Initialize the Project
Open your command prompt in your project directory and type the command below:
Install required modules.
The following modules are going to be needed to successfully build the app.
- express: used to create handle routing and process requests from the client.
- express-fileupload: Simple express file upload middleware that wraps around busboy.
- body-parser: used to parse incoming request from the client.
- mysql: Node JS driver for MySQL.
- ejs: templating engine to render html pages for the app.
- req-flash: used to send flash messages to the view
- nodemon: Installed globally. It is used to watch for changes to files and automatically restart the server.
Type the following command to install the first 7 modules as dependencies.
Then type the following command to install the last module globally on your PC.
Creating the database for the app
Copy the command below and navigate to your phpmyadmin dashboard and execute the following query in the console (usually found at the bottom of the page) in order to create database and table for the app.
Adding the views
The header.ejs file is going to be in the /views/partials folder where it is going to be included in the rest of the project.
This is the homepage of the app which contains a table to display a list of all the players.
This page contains the form to add a new player to the database.
This page contains the form to edit a player added to the database.
Working on the server.
In the above code, the modules are required and then a connection to the database is created. The mysql.createConnection function takes in an object which contains the configuration of the database being connected to. In the next statement, the database is being connected. Run the code below on the command prompt to run the server.
Your console should show the result below:
Adding the routes
Copy the code below into the index.js file in the /routes directory.
The db.query function queries the database. It takes in the query and string and a callback which takes in two parameters, if the query is successful, the result is passed to the view in the res.render function.
The player.js file is going to contain all the routes for the players page such as adding a player, updating a player's details and deleting a player.
This file handles all the post and get requests for the players page. The add player function contains a function that uploads the player's image to the /public/assets/img directory and sends the player's details to the database.
Connecting the routes and the views
Go to app.js and uncomment the following lines
- on line 8 and 9
- on line 40 - 47
Running the app
After removing the commented lines, check your command prompt to ensure your code has no errors, then head over to your browser and open http://localhost:5000. The index page will be shown and since no players have been added, the page will look similar to the one below:
Click the Add a player link on the page, the add player page will load and then fill the form to add a player. After adding a player the home page will display the players added in a table. Like this
If errors are encountered, draw my attention in the comment section or check the repo of the project on github.
The code is not perfect and please feel free to make it better and send a pull request.
Don't forget to check me out on twitter or github.