0% found this document useful (0 votes)
7 views

Lab04_Express_API_MySQL

The document outlines a lab exercise for creating and consuming an API using Express.js and MySQL with Axios. It includes detailed steps for setting up a MySQL database, creating a user, and implementing CRUD operations for a 'students' table. Additionally, it provides instructions for testing the API using Postman and modifying student records.

Uploaded by

pcmohamed28
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab04_Express_API_MySQL

The document outlines a lab exercise for creating and consuming an API using Express.js and MySQL with Axios. It includes detailed steps for setting up a MySQL database, creating a user, and implementing CRUD operations for a 'students' table. Additionally, it provides instructions for testing the API using Postman and modifying student records.

Uploaded by

pcmohamed28
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16
Node.JS — Express.JS Lab05 : Consommer une API a l’aide de la librairie Axios Objectif : 1. Créer une API en utilisant Express.js et MySQL 2. Consommer I’API avec Postman 3. Développer une application Web pour consommer I’API a l'aide de la librairie AXIOS Travail a faire Partie | I-Lancer MySQL Workbench et se connecter au serveur MySQL avec le compte root 2-Créer le compte nodejs_user avec le mot de passe topsecret et lui attribuer le role administrative DBA. a- Utiliser la commande sql: CREATE USER 'nodejs_user'@'localhost' IDENTIFIED BY 'topsecret'; b- Vous pouvez aussi utiliser I'interface de WorkBench. Users and Privileges Attribuer le role administratves DBA au compte nodejs_user a-Utiliser la commande : GRANT ALL PRIVILEGES ON *.* TO ' nodejs_user '@'localhost’; b-Utiliser I'interface de WorkBench. Hassouni Larbi ‘API avec Express.js~ MySQL 1 ‘Stars ond Privileges 3-Déconnecter le compte root en exécutant la commande File/close connection tab 4-Créer une connexion au serveur MySQL pour le compte nodejs_user en cliquant sur le bouton + Welcome to MySQL Workbench Remplisser la fenétre qui s’affiche comme suit : Hassouni Larbi ‘API avec Express.js~ MySQL 2 Bl setup new connection =a x Covecentne: rte, wees ot: [Bend TP) tba ee rc eS hewmen 5a. heme vesmere: 7001 ro so rea net eerste emane: dee Neve tee ct —— = ‘Theumspuamort Wheres ar Fs fad hme feet a Calors ave Renee coat) 0 5-Se connecter au serveur MySQL avec le compte nodejs_user 6-Créer la base nodejs_db en exécutant la commande CREATE DATABASE nodejs_db; 7-Utiliser la base de données nodejs_db que vous venez de créer use nodejs_db; 8-Créer la table students en exécutant la commande create table students( id integer auto_increment, nom varchar(25) , prenom varchar(25) , note real, constraint pkstudents primary ke \ 9-Insérer les lignes de données en exécutant le commandes insert into students(nom, prenom, note) values ('Moha’, ‘Ali’, 15); insert into students(nom, prenom, note) values ('Manolo’, ‘Najib’, 17); insert into students(nom, prenom, note) values (‘Toto’, ‘Adil’, 14); Hassouni Larbi ‘API avec Express.js— MySQL Partie Il 1-Dans le dossier E:\MyNodelS Créer le dossier du projet sour le nom API_MySql Reet] Ge Ome Clem e SAO UNCP eee mest) Le systéme modifie le fichier package.ison pour lui ajouter des dépendances aux modules installés : Hassouni Larbi API avec Express.js- MySQL. 4 an ced Le systéme ajoute le fichier package-lock,json et le dossier node_modules ou i stocke les librairies des modules installés. Créer le fichier config.json qui définit quelques paramétres d’initialisation tel que le numéro de port utilisé par le serveur. Créer le fichier app01 js contenant le code ci-dessous Creer ruc eres Pa Gna ner Ce nfig') Role tastl et eye Soars aoe Hassouni Larbi API avec Express.js- MySQL. 5 connected Ae Ajoutez le code ci-dessous au fichier app.js Gee) Arréter le seveur puis exécuter le fichier app,js Lancer votre navigateur puis accéder & I'url : http://127.0.0.1:3000/students Vous devez obtenir la réponse ci-dessous Hassouni Larbi API avec Express.js- MySQL. 6 x + © 1270013000/sudens A & O @ © Unversies C3 Abonnement » impression élégantea Ct 4 id": 1, “nom: “Manolo”, “prenom": "Najib", “note": 17 id": 2, nom": "Manolo" “prenom": "Najib", "note": 17 aids Ss, "nom": "Toto", “prenom" “note”: 14 Arréter le seveur puis exécuter le fichier app.js. Lancer votre navigateur puis accéder a I’url : http://127.0.0.1:3000/students/id Hassouni Larbi API avec Express.js- MySQL. 7 Remplacer id par une valeur qui se trouve dans la table members. Vous devez obtenir une réponse quia le format ci-dessous : :3000/students/1 x oe = Oo x 1:3000/students1 Q x OF @: » | © Tous les favoris ;€ > @ Or © Dictionary (4 Universities [4 Abonnements Impression élégante £ Sivous donnez un id qui n’existe pas, vous obtiendrez la réponse ci-dessous : YB wro0.13000sudeRss = Xt € > S © 1270013000/studentys Axe OF @ © Dictionary (2) Universities [2 Abonnements » © Tous tes favoris Impression élégante© gO % Hassouni Larbi ‘API avec Express.js— MySQL Pour ajouter un nouveau student & la table students, ajouter au fichier app.js le code ci-dessous. «json() ery eRe Nos] query("INSERT INTO students(nom, prenom, n VNR GD Reese if (err) { le.log(err); aerstrye Eat age: ‘Inserted success Pour tester votre méthode, il faut installer I'utiitaire postman, Télecharger postman a I'url: https://www,postman.com/downloads/ Hassouni Larbi API avec Express.js- MySQL. 9 Download Postman Download the app to get started using the Postman API Platform tod browser experience, you can try the web version of Po! The Postman ape a. Vous pouvez aussi ajouter I'add-in postman a votre vscode ond Postman eet Pour ajouter un nouveau membre avec postman, lancer postman puis remplisser sa fenétre en respectant ordre comme ci-dessous Hassouni Larbi API avec Express.js- MySQL. 10 Ajout d’un étudiant en utilisant un formulaire : Dire a Tame se reer eT re tte re me orl tit Tis iret cay Se Caete cee Bape ayaa id= fous Eos Cara pass id= fru Ese Tegan pas Cru aaa Neston ca PNY ire Nom: Manolo Prénom: qwerty Note: 10 Envoyer Impression élégante "message": "Inserted successfully"} Hassouni Larbi API avec Express.js- MySQL. cE Pour modifier un student qui exite dans la table students, ajouter au fichier app.js le code ci-dessous. areas Pata if (err) { ee Contenu de la table students : id fom prenom note 1 Manolo —sNajb 17 2 Mandlo = Nab 17, 3 Toto Adi 4 semaoune _meryem _ 20 Pour modifier un student avec postman, lancer postman puis remplisser sa fenétre comme ci- dessous (Modification du student d’id = 5). Hassouni Larbi API avec Express.js- MySQL. 2 Hassouni Larbi API avec Express.js- MySQL. 3B Pour supprimer un student qui exite dans la table students, ajouter au fichier app,js le code ci- dessous, oer @W ae eerie te Vee Ca PS) een erika mea Omen (err, resu Poa coe) if (result[o] -query("DELETE FROM sti = rong id’ }) Commengons par exécuter la requétte GET pour consulter tout le contenu de la table students, Hassouni Larbi API avec Express.js- MySQL. 4 LJ & 12700.1:20 o/students x + < S © 127.0.0.1:3000/students © Dictionary (5 Universities [9 Abonnements Impression élégante C t id": 1, nom": "Manolo", "Najib", i 17 3 € id": 2 nom": "Manolo", prenom": "Najib", : 17 i t id": 3. nom Toto", prenom": "adil", 4 14 i t adr: nom": "ardoune", prenom": “nadia”, 18 I i Sp > 1 oe 2 | CO Tous tes favoris Hassouni Larbi ‘API avec Express.js— MySQL 15 Vérifions que le membre ayant I'id 5 a bien été supprimé en exécutant la requéte GET ¥ — & 127.00.13000/studer x + - o x € © © 127.0.0.1:3000/students/5 Qe Oo @O: © Dictionary (9 Universities (9 Abonnements » Impression élégante a (1 @ Charchons a supprimer un id (id=9) qui n’existe pas Hassouni Larbi ‘API avec Express.js— MySQL 16

You might also like