Create Fullstack App Fewvlearns - Backend

  1. GitHub Repository Link

  2. Download MySQL Workbench

  3. Download MySQL Server

» Prerequisites

You are going to need MySQL and its workbench installed on your local machine. For it download mysql and MySQL Workbench

After it’s done installing, configure it properly.

» Create AWS RDS

We will start off by creating mysql db instance. For it we will use Amazon RDS to create a MySQL DB Instance. And this is going to be Free Tier eligible. So you don’t need to worry.

Open the AWS Management Console in a new browser window, and in the search bar search for database and then choose RDS

In the top right corner of the Amazon RDS console, select the Region in which you want to create the DB instance.

In the Create database section, choose Standard Create database.

Then select standard create in choose a db creation method.

Then you now have options to select your engine. For this tutorial, choose the MySQL icon, leave the default value of edition and engine version, and select the Free Tier template.

For Master username: Type a username that you will use to log in to your DB instance. I’ll will use fewvlearns in this example.

Master password: Type a password fewvlearns-master-password

Confirm password: Retype your password

  • Now we have to set Instance specifications:

    • DB instance class: Select db.t2.micro — 1vCPU, 1 GiB RAM. 

    • Storage type: Select General Purpose (SSD). For more information about storage, see Storage for Amazon RDS.

    • Allocated storage: Select the default of 20 to allocate 20 GB of storage for your database. You can scale up to a maximum of 64 TB with Amazon RDS for MySQL.

    • Enable storage autoscaling: you will enable it if you want Amazon RDS to automatically scale up your storage when needed. We won’t do this for this project.

    • You are now in the Connectivity section where you can provide information that Amazon RDS needs to launch your MySQL DB instance.

    • Compute resource: Choose Don’t connect to an EC2 compute resource. You can manually set up a connection to a compute resource later.

    • Virtual Private Cloud (VPC): Select Default VPC. 

    • Subnet group: Choose the default subnet group. For more information about subnet groups, see Working with DB Subnet Groups.

    • Public accessibility: Choose Yes. This will allocate an IP address for your database instance so that you can directly connect to the database from your own device.

    • VPC security groups: Select Create new VPC security group. This will create a security group that will allow connection from the IP address of the device that you are currently using to the database created.

    • Availability Zone: Choose No preference. See Regions and Availability Zones for more details.

    • RDS Proxy: By using Amazon RDS Proxy, you can allow your applications to pool and share database connections to improve their ability to scale. Leave the RDS Proxy unchecked.

    • In additional configuration, for Port: Leave the default value of 3306.

  • Monitoring

    • Enhanced monitoring: Leave Enable enhanced monitoring unchecked to stay within the Free Tier. Enabling enhanced monitoring will give you metrics in real time for the operating system (OS) that your DB instance runs on. For more information, see Viewing DB Instance Metrics.

    • In the Additional configurations section:Database options

    • Database name: Enter a database name, I’ll name it fewvlearns-database. If you do not provide a name, Amazon RDS will not automatically create a database on the DB instance you are creating.

    • DB parameter group: Leave the default value. For more information, see Working with DB Parameter Groups.

    • Option group: Leave the default value. Amazon RDS uses option groups to enable and configure additional features. For more information, see Working with Option Groups.

    • Encryption: This option is not available in the Free Tier. For more information, see Encrypting Amazon RDS Resources. Backup

    • Backup retention period: You can choose the number of days to retain the backup you take. For this tutorial, set this value to 1 day.

    • Backup window: Use the default of No preference.

  • Maintenance

    • Auto minor version upgrade: Select Enable auto minor version upgrade to receive automatic updates when they become available.

    • Maintenance Window: Select No preference.

  • Deletion protection: 

    • Turn off Enable deletion protection for this tutorial. When this option is enabled, you’re prevented from accidentally deleting the database.Choose Create Database.

    • Your DB instance is now being created.Note: Depending on the DB instance class and storage allocated, it could take several minutes for the new DB instance to become available.

    • The new DB instance appears in the list of DB instances on the RDS console. The DB instance will have a status of creating until the DB instance is created and ready for use. When the state changes to available, you can connect to a database on the DB instance. 

    • Feel free to move on to the next step as you wait for the DB instance to become available.Once the database instance creation is complete and the status changes to available, you can connect to a database on the DB instance using any standard SQL client. In this step, we will download MySQL Workbench, which is a popular SQL client.

» MySQL Workbench

Open the workbench app and at the top navigation you will have an option to connect the database, select it.

A dialog box appears. Enter the following:

  • Hostname: For hostname name here, we have to enter the hostname which we will get from the Amazon RDS console. So head back to the console and select you database and there in connectivity and security you will have the endpoint section, grab this and provide it in mysql workbench.

  • Port: The default value should be 3306.

  • Username: Type in the username you created for the Amazon RDS database. In this tutorial, it is ‘fewvlearns’

  • Password: Choose Store in Vault (or Store in Keychain on MacOS) and enter the password that you used when creating the Amazon RDS database. fewvlearns-master-password

Choose OK.

You are now connected to the database! On the MySQL Workbench, you will see various schema objects available in the database. Now you can create tables, insert data, and run queries.

» Error Handling in connecting to DB

But here some might get error and you might not be able to connect to RDS through the workbench. So, for it, what we can do is, head back to the aws console. Search for your DB vpc and then scroll down until you see security tab, and then select security group and then select your security group and then edit the inbound rules. And then click on add rule and here provide your ip address. For ip address you can visit the site : https://whatismyipaddress.com/

» Create DB Tables

Now we will create all the tables that we need in mysql workbench.

Creating the courses Table:

First, we’ll create a table to store all the information about the courses available on our platform. This table is named courses.

CREATE TABLE `courses` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `vimeoVideoId` varchar(255) DEFAULT NULL,
  `syllabus` text,
  `instructor_bio` text,
  `testimonials` text,
  PRIMARY KEY (`id`)
) 

Creating the Purchased Courses Table:

Next, we need a table to track which users have purchased which courses. This is our purchased_courses table.

CREATE TABLE `purchased_courses` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` int DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `course_id` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `purchased_courses_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) 

Creating the Users Table:

Finally, we have the users table, which stores all the user data.

CREATE TABLE `users` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `refresh_token` text,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`)
) 

So, these are the three tables we’ll be using: courses, purchased_courses, and users. They’ll store all the critical data for our application. Once you have these tables set up in your database, you’ll be ready to start working on the backend to connect everything together.

Enter The Data:

INSERT INTO `courses` (`name`, `description`, `vimeoVideoId`, `syllabus`, `instructor_bio`, `testimonials`)
VALUES (
  'Introduction to SQL',
  'This course provides a comprehensive introduction to SQL, the standard language for relational database management systems. Learn how to create, read, update, and delete data in a database.',
  '123456789',
  'Module 1: Basics of SQL\nModule 2: Advanced Queries\nModule 3: Database Design\nModule 4: SQL Optimization',
  'John Doe is a seasoned database administrator with over 15 years of experience in SQL and database management. He has worked with various industries to optimize their database performance.',
  '“This course changed the way I think about data!” - Alice Smith\n“Excellent introduction to SQL with practical examples.” - Bob Johnson'
);
INSERT INTO `courses` (`id`, `name`, `description`, `vimeoVideoId`, `syllabus`, `instructor_bio`, `testimonials`)
VALUES
('1', 'Learn About Kafka and Node.js', 'Introduction to Kafka and Node.js', '1002576242', 
'1. Introduction to Kafka\n2. Setting up Node.js\n3. Integrating Kafka with Node.js\n4. Real-time data processing\n5. Advanced Kafka features',
'John Doe is a seasoned software engineer with over 10 years of experience in distributed systems and real-time data processing.', 
'This course was extremely helpful! - Jane Smith\nHighly recommend for anyone looking to learn Kafka. - Robert Brown'),

('2', 'React, but with webpack', 'Learn React with Webpack', '946135113', 
'1. Introduction to React and webpack\n2. Setting up a webpack project\n3. Configuring webpack for React\n4. Optimizing your build\n5. Advanced webpack features',
'Jane Smith is a front-end developer specializing in modern JavaScript frameworks and build tools.', 
'The best webpack course I have taken! - Alice Johnson\nHelped me understand the intricacies of React and webpack. - Michael Davis'),

('3', 'Learn About Terraform in Depth', 'Deep dive into Terraform', '946135113', 
'1. Introduction to Terraform\n2. Setting up your first Terraform project\n3. Managing infrastructure with Terraform\n4. Advanced Terraform concepts\n5. Real-world use cases',
'Mike Johnson is a cloud engineer with extensive experience in infrastructure as code and cloud automation.', 
'This course made Terraform so much easier to understand. - Emily Clark\nGreat course for anyone new to Terraform. - David Martinez'),

('4', 'Kubernetes and Docker for deployment', 'Master Kubernetes and Docker', '990963368', 
'1. Introduction to Kubernetes and Docker\n2. Setting up a Kubernetes cluster\n3. Deploying applications with Docker\n4. Scaling and managing applications\n5. Advanced Kubernetes features',
'Emily Davis is a DevOps engineer with a strong background in containerization and orchestration technologies.', 
'A must-take course for Kubernetes and Docker enthusiasts. - Jessica Taylor\nVery informative and practical. - Daniel Anderson'),

('5', 'Create your own Serverless web app', 'Build a serverless web application', '946135113', 
'1. Introduction to Serverless\n2. Setting up your first serverless project\n3. Deploying serverless applications\n4. Managing serverless functions\n5. Best practices and real-world examples',
'Robert Brown is a software architect with a focus on serverless computing and cloud-native applications.', 
'Excellent course on serverless architecture! - Laura Thomas\nHelped me build my first serverless app. - Andrew White');

» Set up ENV file:

# Server configuration
PORT=3000

# Database configuration
DB_HOST=
DB_USER=
DB_PASSWORD=
DB_NAME=

# JWT Secret keys
SECRET_KEY=
REFRESH_SECRET_KEY=

# Stripe API key
STRIPE_SECRET_KEY=

# Vimeo API credentials
VIMEO_CLIENT_ID=
VIMEO_CLIENT_SECRET=
VIMEO_ACCESS_TOKEN=

# Client URL
CLIENT_URL=

» Create App on Vimeo

Create App on Vimeo:

Now we will also be needing stripe secret key. For that, you should have a stripe account. Head over to stripe’s website and create a free account and then we will start by Registering our app. To register the app:

  1. Go to the My Apps page.
  2. Click Create an app. Your browser goes to the Create a New App page.
  3. In the App name field, type the name of your app.
  4. In the App description field, type a brief description of your app. This text appears in the authorization prompt, which your end users receive when your app requests permission to access their Vimeo account.
  5. Set the access permissions to your app under Will people besides you be able to access your app? by selecting either No (only you can access your app) or Yes (other Vimeo accounts can access your app).
  6. At the bottom of the form, check the confirmation checkbox to agree to the Vimeo API License Addendum and the Vimeo Terms of Service.
  7. Click Create

Now that’s done, we will Generate an access token

Click the app name and it will take you to this app’s information page, here in the navigation list on the left side of the page, click Generate an access token, or just scroll down the page to this section.

  1. If you want to be able to access your private data, select the Authenticated (you) option. This generates the Scopes section, which offers various scopes. You must check the private scope to proceed. If you don’t select a token type, your token defaults to Unauthenticated, which is public.
  2. Select authenticated > Private > videos
  3. Click Generate. The new token appears in the Personal Access Tokens table.
  4. Copy the token string, and paste it into the env file.
  5. Here, you will also have client secret, grab that, and paste it into the env file.
  6. For client id, it’s present at the top of the page, grab that and paste into env file
  7. Grab all the keys.

» Stripe API key

For stripe secret key, visit stripe’s website, create an account and then visit stripe’s dashboard, make sure, you’re in test mode, and then here, you will have two keys, publishable and a secret, grab your secret key and paste it in env file.

» Create Backend

Install Dependencies:

cd server
npm init -y
npm install bcrypt body-parser cors dotenv express jsonwebtoken mysql2 nodemon stripe vimeo

Copy all of these below given files one by one in your project folder.

Visit Github Repository (Link Given Above) and Copy the code for backend.

└── server
    ├── config
    │   └── database.js
    ├── controllers
    │   ├── authController.js
    │   ├── checkoutController.js
    │   ├── courseContentController.js
    │   ├── courseController.js
    │   ├── courseDetailsController.js
    │   ├── purchasedCoursesController.js
    │   ├── storePurchaseController.js
    │   └── videoController.js
    ├── middlewares
    │   └── authenticateToken.js
    ├── package-lock.json
    ├── package.json
    ├── routes
    │   ├── authRoutes.js
    │   ├── checkoutRoutes.js
    │   ├── courseContentRoutes.js
    │   ├── courseDetailsRoutes.js
    │   ├── courseRoutes.js
    │   ├── purchasedCoursesRoutes.js
    │   ├── storePurchaseRoutes.js
    │   └── videoRoutes.js
    ├── server.js
    └── services
        ├── stripeClient.js
        └── vimeoClient.js