Express js: The Ultimate Beginner's Guide to Learn Express js Step by Step 2020 (2st Edition)


part that will communicate with the server to fetch query results from the


Download 316.23 Kb.
Pdf ko'rish
bet3/4
Sana15.08.2023
Hajmi316.23 Kb.
#1667262
1   2   3   4
Bog'liq
Express JS


part that will communicate with the server to fetch query results from the
database and provide it to our project. Some of the modules offering
connectivity to the MySQL database server, the best of which is mysql that
can be installed by executing the command:
npm install mysql --save
Tip: You can install two packages with one command:
npm install express mysql --save
After installing the two packages, we will notice that a new field has been
added in the package.json file that explains the project requirements we
have saved so far (your version numbers may vary):
"dependencies": {
"express": "^ 4.10.6",
"mysql": "^ 2.5.4"
}
Create the database and enter some entries as a sample
Having some posts will help us build the interface and see the changes more
easily, so we will create the database and enter some entries to it first of all.
To create the database and make entries, we will communicate with the
server via the terminal, using the mysql program that is automatically


installed when installing MySQL or MariaDB. Open the terminal and run
the command:
mysql -u root
Note: If you choose a different user name and password during installation,
you can enter it in the following way (you will be asked to enter the
password for this user):
mysql -u username -p
Note: If you have problems starting mysql, try restarting the system. Note
(2): If you have installed PHPMyAdmin on your computer, you can do
away with the MySQL shell, where you can enter the same commands in
the query box.
In this MySQL shell, we can enter MySQL commands for the server to
execute immediately. We'll create a blog database, and we'll call it myblog:
CREATE DATABASE myblog;
The following result is evidence of successful implementation:
Query OK, 1 row affected (0.00 sec)


Connect to the new database:
connect myblog
connect myblog
Let's create a table for users and another for posts and then enter a user with
4 posts written on different days, you can just copy and paste it into
MySQL shell:
CREATE TABLE `users` (id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT, username VARCHAR (50), password VARCHAR
(500) NOT NULL, full_name VARCHAR (50), is_author BOOLEAN
DEFAULT, UNIQUE INDEX (username));
INSERT INTO `users` (username, password, full_name, is_author)
VALUES (" admin "," $ 2a $ 08 $
Z3FpAQwRgj7W0i71TtizFO7QDjpsIRNJfHh6mLgRJRJBtheKJh1Tu ","
admin ", 1);
CREATE TABLE `posts` (id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT, title VARCHAR (100), body LONGTEXT, date
TIMESTAMP, author_id INT, slug VARCHAR (50), UNIQUE INDEX
(slug), FOREIGN KEY (author_id) REFERENCES` users` (id));
INSERT INTO `posts` (title, body, date, author_id, slug) VALUES (" Hello
world! "," Welcome to my humble blog! "," 2014-12-29 ", 1," hello-world
");
INSERT INTO `posts` (title, body, date, author_id, slug) VALUES ("
Quotes (1) "," Rich if asked about improving work and life will say: We
know that misery is unpleasant. But we do not expect us to do anything
about it, we are sorry for your lower classes like we are sorry for strangers


... However, we will fight like apostasy against any improvement of your
circumstance.We feel that you are safer in this situation. Ready to risk your
release even an extra hour a day, dear brothers, if you have to sweat to pay
for our trips to Italy. - George Orwell, homeless in Paris and London ","
2014-12-30 ", 1," quotes-1 ");
INSERT INTO `posts` (title, body, date, author_id, slug) VALUES ("
Quotes (2) "," TV plunges you into a sea of sounds and colors so you don't
have time to think or criticize ... it offers you ready ideas and no Allows you
to criticize what the book allows.- Ray Bradbury, Fahrenheit 451 "," 2014-
12-31 ", 1," quotes-2 ");
INSERT INTO `posts` (title, body, date, author_id, slug) VALUES ("
Quotes (3) "," I can tell you, my son, that happiness is a fountain that
explodes from the heart, no one is falling from heaven, Of the vices and
vices of the vices, and the ambitions of life and lusts, happy wherever it
comes. - Mustafa Lotfi Manfalouti, virtue "," 2015-01-01 ", 1," quotes-3 ");
Each line in the post table has the following fields: title, body text, date,
author_id, which indicates one of the authors in the users table, and slug,
the appropriate English address to use within the blog link, such as hello-
world in http: // myblog / posts / hello-world.
We are now ready to work! In the next lesson we will start by creating the
main page of our blog, which will show the posts we just added.


Chapter One
Routing links in Express
In the previous lesson we installed Node.js, MySQL server and other
project requirements, it's time to get started!


Create a blog master page
The main page of each blog usually shows the latest posts on the date they
were written from the most recent to the oldest, and we will now focus on
the implementation of this section that we will expand the addition of
features later.
Create the index.js file, which is the starting point of our project.

Download 316.23 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling