Google Contacts FullStack Clone With HTML5+SASS+Javascript + Nodejs + Express.js + MySQL – Part 12
Welcome to Part 12 of our Google Contacts FullStack Clone tutorial series. In this part, we will be focusing on integrating MySQL database into our application using Nodejs and Express.js.
Setting up MySQL Database
First, make sure you have MySQL installed on your machine. You can download and install MySQL from their official website.
Once MySQL is installed, create a new database for our project. You can use the following SQL command to create a new database:
“`sql
CREATE DATABASE google_contacts;
“`
Next, create a new table to store our contacts. You can use the following SQL command to create a contacts table:
“`sql
CREATE TABLE contacts (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
“`
Connecting Nodejs and MySQL
In order to connect our Nodejs application to the MySQL database, we will be using the `mysql` package. First, install the `mysql` package using the following command:
“`shell
npm install mysql
“`
Next, create a new file called `db.js` and add the following code to establish a connection to the MySQL database:
“`javascript
const mysql = require(‘mysql’);
const db = mysql.createConnection({
host: ‘localhost’,
user: ‘root’,
password: ‘yourpassword’,
database: ‘google_contacts’
});
db.connect((err) => {
if (err) {
console.error(‘Error connecting to MySQL database: ‘ + err.stack);
return;
}
console.log(‘Connected to MySQL database’);
});
module.exports = db;
“`
Now, we have established a connection to the MySQL database using Nodejs. We can now perform CRUD operations on the `contacts` table using Nodejs and Express.js.
Conclusion
In this part, we have learned how to integrate MySQL database into our Google Contacts FullStack Clone application using Nodejs and Express.js. We have set up the database and established a connection to it, allowing us to perform CRUD operations on our contacts table.
Stay tuned for the next part, where we will be implementing the functionality to add, edit, and delete contacts from our application using the MySQL database.
Thank you for reading!
Can you please do more fullstack related projects..?
Thank you