CRUD Operation using NodeJS ExpressJS MySQL – Part 1

This post explains how we can build a CRUD using NodeJS ExpressJS MySQL database. Here we are going to use Express Framework and EJS as a template engine to build a CRUD operation. For CRUD operation we will use HTTP Methods to make our application. We will post and retrieving data using MySQL database.

Step 1: Install nodeJs package and Init application

Initially download NodeJS and install in your system and confirm everything configured correctly using the following command.

Step 2: Create a directory and init application

Create an applicate folder and init the project using the following command.

Step 3: Install required packages using NPM

The following modules are going to be required to create the application.

  • Express: used to create manage routing and process HTTP requests from the end client.
  • body-parser: used to parse incoming request from the end client.
  • mysql: Node JS driver to connect MySQL and perform CRUD operations.
  • ejs: is templating engine and its used to render HTML pages to end client
  • nodemon: Optional package and Installed globally. It helps us to listen for modifications to files and automatically restart the app server.

Run the following command to install the above mention modules as dependencies for our application.

Step 5: Create app.js file

Create app.js server file and include the required dependency package in it for our CRUD application. And define the server port and write an app server .listen() function.

Step 6: Create a Database Connection

Create a new database with the following name node_crud and  If you create it with the same name it’s better.
To create a database in MySQL, execute the following query:

After creating a database, we need to create a table with the following name users. If you create it with the same name it’s better. To create a table in Database, execute the following query:

To define a MySQL connection add following lines in app.js file.

Step 7: Define View engine with ejs / public path / View files path

Here we are going to define a template view engine with ejs using set() and use() function and also define a Public path and View folder path location.

Step 8: Define view HTML files

Here we are going to define HTML page with ejs extension and adding HTML content in it. We created four HTML files for user CURD operation. I am going to create file with named as user_index.ejs.

Step 9: Define ‘/’ URL path with User listing procedure

Here we are going to add the Home path and define user index page with displaying user list in it.

Step 10: Run the application and verify with browser

Here we are going to run an application with the following command to test our CRUD application in a browser.

Finally app.js file code following details.

Run the Application using the following command.

 

Now you are ready to see the demonstration of CRUD using NodeJS ExpressJS MySQL.

Download  Git Repository

 

Learn Infinity

Learn Infinity is the most famous Programming & Web Development blog. Our principal is to provide the best online tutorial on web development. We execute the best tutorials for web experts this will help developers, programmers, freelancers and proving free resource you can download or preview the tutorials.

Leave a Reply

Your email address will not be published.

Back to top