Example to create RESTFul API for CRUD operations using MySQL as Database. Using Babel to transform modern Javascript syntax to versions that any browser can understand.
You will be able to test the differents endpoints that I wrote and make the classics request methods as GET, POST, PUT and DELETE operations.
You need to have basic knowledge of the technologies used in the creation. I use
Download the project and open it in your code editor. And put on the console
npm install
npm start
If everything went well, you should see something like this on the console
Server Running in Port xxxx
Database Connected
In this point we are more than ready to make the tests!!
Before make test check the file with name createcompanyDB.sql en run the commands in Workbench o console
For test in my case I use POSTMAN
Main URL: localhost:xxxx/company
This request will return the complete list of company. GET method: localhost:xxxx/company
The answer will be something like:
[
{
"id": 1,
"first_name": "Flavio",
"last_name": "Aro",
"email": "flavio.aro@gmail.com",
"age": 33
},
{
"id": 2,
"first_name": "Teste",
"last_name": "Teste",
"email": "teste@gmail.com",
"age": 30
}
]
This request will return only 1 company. GET by ID method: localhost:xxxx/company/id/:id
The answer will be something like:
{
"id": 1,
"first_name": "Flavio",
"last_name": "Aro",
"email": "flavio.aro@gmail.com",
"age": 33
}
This request will create a company. POST method: localhost:xxxx/company/add
The company information you enter on Postman should look something like:
{
"first_name": "You new company name",
"last_name": "company last name",
"email": "company@examplemail.com",
"age": The company age as number value
}
This request will be able to update the data of a specific company. PUT method: localhost:xxxx/company/edit/:id
You can change the data of a specific company for example:
{
"first_name": "Flavio",
"last_name": "Aro",
"email": "flavio.aro@gmail.com",
"age": 33
}
to:
{
"first_name": "Flavio",
"last_name": "Aro",
"email": "flavio.aro@gmail.com",
"age": 33
}
This request will remove a specific company. DELETE by ID method: localhost:xxxx/company/delete/:id
This request will remove all records from the company table. DELETE method: localhost:xxxx/company/deletecompany