Creation of Web Application with Node JS and Angular V6 — Part 1 (BACKEND)
In this tutoriel we are going to set up an application which is going to allow us to list and create subjects.
We are going to use architecture third parties for our application:
BACKEND (REST API) : NODE JS — EXPRES.JS
DATABASE : MYSQL
FRONTEND: ANGULAR V6
BACKEND
TOOLS
MYSQL
DATABASE SCHEMA
Below the schema our database, we have 3 tables t_matiere, t_etudiant and t_note.
DATABASE CREATION
We go on PHPMYADMIN to import our database for the address below:
DOWNLOAD THE DATABASE AND IMPORT IN PHPMYADMIN
We are then going to download the schema of our database on github (below link of the github repository) before of the importer with phpmyadmin.
NODE JS
INSTALLATION
To install NODE JS, click the link below:
VERIFICATION
To verify that NODE JS is installed well thrown this command:
node -v
PROJECT CREATION
Execute the command below to create the directory of our project NODE JS.
mkdir NoteProjetNode
PROJECT INITIALISATION
The commands below we shall allow to initialize our NODE JS project:
cd NoteProjetNode
npm init
INSTALL DEPENDENCES
Then we are going to install the dependences which we need:
body-parser : This library will allow us to decode the data JSON sent by the FRONT to the BACK.
express : This library will be used to expose our Web services(rest api).
express-list-endpoints: This library allows us to list our Web services.
my-sql: Mysql library allow us to communicate with our database.
officegen: This library allows to generate a Word file, we use it to generate the bulletins of the students.
npm install body-parser express express-list-endpoints mysql officegen --save
DATABASE CONNECTION
The code below is going to allow us to connect in our database mysql.
IMPLEMENTATION OF MATIERE MODULE
We create a folder matiere.
mkdir matiere
CREATION OF MODEL — matiere/Matiere.js
Then we create a file Matiere.js, which is the model of the module matiere. It is in this file that will be the part business by our module. Here we communicate with a database MYSQL but we can communicate with API (for example the api of Facebook or twitter).
We have 2 methods:
getmatieres : This method returns the list of all subjects in our database.
creatematiere: This method allows us to create a subject.
CONTROLLER CREATION — matiere/MatiereController.js
Here we have 2 services:
get: We use the method getmatieres of the model Matiere to return all the subjects.
post: The method creatematiere of the model Matiere which allows us to create a subject.
WEB SERVICES EXPOSITION — app.js
We return our available services by registering them with express.
IMPLEMENTATION OF SERVER— server.js
We set up our nodeJS server.
START THE PROJECT
Execute the command below to start our project.
node server.js
STRUCTURE OF PROJECT
TEST THE SERVICES WITH CURLS
We can test our services with CURL:
The first command allows us to test the service recovery of all the subjects and the last one the service of creation of material subject.
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:3000/matierescurl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://localhost:3000/matieres -d '{"libelle":"EPS","coefficient":"2"}'
TEST THE SERVICES WITH POSTMAN
We can also use POSTMAN to test our services, below an example;
GITHUB
Link GitHub of the project:
After the clone of the repository, you have to execute the commands below:
npm install
node server.js
IMPROVEMENT
Create the methods Update and Delete Matiere.