MySQL
- Install mysql driver for express/node application.
npm install mysql
- Install MySQL
Before going to use the local database, you should have mysql installed in your local system, proceed to MySQL official links given below, they’ve provided a simple procedure for installation.
Click Here for Installation:- Windows | Linux | MacOS
After installation you will be able to use the MySQL database with your terminal also you can use any GUI like PHPMyAdmin and MySQL workbench, etc to interact with the Database.
- Integrate with the express application
Explanation:- First of all we will import the mysql module and then createConnection method takes some configurations about the database and provides us a connection object in return. This connection object will be used to call the connect method which connects the application with the database and provides an error object on failure and threadID on successful connection. This connection will be terminated either when the program finishes execution or when the connection calls the end method i.e. connection.end().
After the successful connection, we are calling the listen method of the express app which starts the server.
// Filename - app.js
// Importing modules
const express = require('express');
const mysql = require('mysql');
const app = express();
const PORT = 3000;
// Create a connection to the database
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'yourpassword',
database: "ExpressIntegration"
});
// open the MySQL connection
connection.connect(error => {
if (error){
console.log("A error has been occurred "
+ "while connecting to database.");
throw error;
}
//If Everything goes correct, Then start Express Server
app.listen(PORT, ()=>{
console.log("Database connection is Ready and "
+ "Server is Listening on Port ", PORT);
})
});
Output: Start the server with the `node app.js` command and this will be output in the terminal, which means we are successfully able to integrate the MySQL database.
Access Cloud Service
There are lots of service providers with which you can work, here I am going to use the simple one Clever-Cloud MySQL hosting. Click to proceed on the website and then register/sign in yourself, then check out the console of the website. You will see something like this,
- Click on personal space and then on create. Now, choose an addon and then select MySQL from the list of the addons. It will prompt for the name of the database, provide whatever you want, and then select the zone.
- Finally, click on next and now it will provide Database credentials, use these in your express application to access this database also you can export the environment variables to avoid doing this copy-paste stuff. Clever-Cloud also provides a Web User Interface with PHPMyAdmin so you can easily configure and manage your database.
- Now just replace the data written inside the object being passed to the createConnection method with the data & credentials provided by the clever-cloud, and you will be able to access this database.
Note: You may have noticed that we are connecting to the express server only after a successful database connection, it’s just a convention because the application can work well only when each server is running. Although no one is stopping you to start the server after or before the database connection.
Database Integration in Express.js
Express is a minimalistic framework that works along with Node.js and provides salient features to run a backend server and much more. As you know the database plays a vital role in a fully working application to persist data. In this article, we are going to discuss how we can integrate a database in a server that is working upon the Express.js framework.