Deploy your first Node.js CRUD Application on cPanel with Newsys Managed Service
In this tutorial, we will build a super simple Node.js Express application for CRUD (Create, Read, Update, Delete) operations and we will use MySQL managed database service along with the Node.js hosting service on cPanel with Newsys Web Hosting for hosting our application.
Overview of the CRUD API
First, we will start with an Express web server and we will add the database configuration for MySQL database and create a Testimonial model and a controller to control the module. Then we will define routes for the CRUD operations.
Crafting the Node.js Application
Login / Create an account with Newsys. For small scale apps, I personally prefer the Basic plan which starts as low as 59/- per month with a .xyz domain for development purposes. But for enterprise usage, I recommend the purchase of the Professional / Unlimited plans.
- After logging in navigate to Services. ( Client Area > Services )
- Click on the product that you have purchased and on the left sidebar ‘Login to cPanel’.
NB: You will also receive a mail regarding the cPanel username and credentials when you order any product.
After logging in to cPanel search for Node.js Application.
— Choose the Node.js version of your choice from the available list and other details as shown in the figure below.
NB: Your Application root will be the folder from which your application will operate.
Boom your application is ready.
You should be able to check your node application at https://<domain-name>. Copy the command highlighted to get started with building the application.
Configure and note your database credentials
Before getting started with the application let us create the database and provide appropriate privileges to the database user.
Scroll down to create a new MySQL User.
NB: Do note the database name, username, password in the process.
After creating, Go back and scroll down again to provide user the appropriate privileges.
The username for me as mentioned in the figure above is cetbin_testuser and the database is cetbin_apibricks.
Creating the table for our testimonials:
- Open phpMyAdmin tool in the cPanel to access the database created via the frontend tool.
Click on the database and then click on SQL to find ‘Run query/queries on database <databasename>’:
Installing the dependencies
NB: Skip this, If you already have your application ready you can directly upload your files with the node_modules folder to the APPLICATION ROOT folder using FileManager.
Search for the Terminal on cPanel and enter the same.
npm init -y
npm install express body-parser cors mysql --save
Adding your code to the Application using File Manager.
Folder Structure under ( /home/<username> )
Your directory structure will be the same as shown in the figure on the left.
Create the folders if not present already using the options provided on the Top menu.
Make sure that you are working on the same folder which you mentioned as your application root.
- Now create the index.js file and populate the code as below.
2. Create the db.config.js file and populate the same as below and save the changes.
3. Define the testimonial routes in the routes folder. (testimonial.routes.js)
4. Define the model in the ‘models’ folder. (testimonial.mode.js)
5. Define the controller in the ‘controllers’ folder. (testimonial.controller.js)
Restart the Node.js server
Test the REST API Service
Hurrah! We just built our first Nodejs application on cPanel with partially managed servers.
Thank you for showing your interest. If you liked it please do like.