Node.js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services but was designed with real-time, push-based architectures in mind.
MySQL is a relational database management system based on SQL – Structured Query Language. The application is used for a wide range of purposes, including data warehousing, e-commerce, and logging applications. The most common use for MySQL, however, is for the purpose of a web database.
Create a database and a database table to work with. You can do this using a graphical interface, such as phpMyAdmin, or using the command line. For this post I’ll be using a database called DB_Role and a table called role. Here’s a dump of the database, so that you can get up and running quickly if you wish to follow along:
CREATE TABLE role (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50),
PRIMARY KEY (id)
);
INSERT INTO role (id, name) VALUES
(1, 'Super Admin'),
(2, 'Admin'),
(3, 'User'),
(4, 'Staff');
Create Node.js Project:
Follow below Steps for Creating Node.js Project,
Connection with MySQL:
Follow below Steps for Connecting MySQL with Node.js Project,
var mysql=require("mysql");
var db = mysql.createPool({
connectionLimit: 1000,
acquireTimeout: 3000000, //30 secs
host: '127.0.0.1',
port:'3307',
user: 'root',
password: '',
database: 'db_role'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
Run Node.js project using node app.js in your terminal.
The output of this project will be:
Here I will explain how you will connect your MySql server with node.js Application.
Before starting,
Create DataBase:
Before starting,
- First, we have to download and install Node.js
- Download and Install XAMPP
- Download and install Visual Studio Code.
- Run npm install command for checking the Node.js module.
Create DataBase:
Create a database and a database table to work with. You can do this using a graphical interface, such as phpMyAdmin, or using the command line. For this post I’ll be using a database called DB_Role and a table called role. Here’s a dump of the database, so that you can get up and running quickly if you wish to follow along:
CREATE TABLE role (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50),
PRIMARY KEY (id)
);
INSERT INTO role (id, name) VALUES
(1, 'Super Admin'),
(2, 'Admin'),
(3, 'User'),
(4, 'Staff');
Create Node.js Project:
Follow below Steps for Creating Node.js Project,
- Open Visual Studio Code and Go to Terminal and click on new Terminal
- Then run cd F:\node_js_app . Here I run this command for redirecting to that directory.
- Run mkdir Roleapp for creating Folder in that location. This one is my project folder.
- Then run cd Roleapp for redirecting to your project folder.
- Run npm init -y for creating package.json file in your project.
- Open that Roleapp folder in Visual Studio Code and you will see package.json file already created.
- Create app.js file.
Connection with MySQL:
Follow below Steps for Connecting MySQL with Node.js Project,
- Open new Terminal and run npm install mysql . For installing MySQL in Node.js.
- Then Copy the following code in your app.js.
var mysql=require("mysql");
var db = mysql.createPool({
connectionLimit: 1000,
acquireTimeout: 3000000, //30 secs
host: '127.0.0.1',
port:'3307',
user: 'root',
password: '',
database: 'db_role'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
Run Node.js project using node app.js in your terminal.
The output of this project will be:
0 Comments