GitHub – FedeBerbara/RestAPI-Node-Express-MySQL: RestfulAPI example with NodeJS and MySQL using modern syntax.

RestAPI template using NodeJS + Express + MySQL + ES6

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.

¿ What you can do with this examples ?

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.

¿ What knowledge do you need to understand this examples ? 🛠️

You need to have basic knowledge of the technologies used in the creation. I use

  • NodeJS – As main language
  • Express – To have our server
  • MySQL – As database to store the data.
  • MySQL Workbench – MySQL graphical interface

If you don’t have MySQL workbench you can check the documentation or look for a tutorial. On Youtube there are many

Installation

Download the project and open it in your code editor. And put on the console

npm install

This command will install the necessary dependencies for the project.

After that you need to create your .env file following the .env.example file that I left. You will need it for the connection to MySQL and then put in the console

npm start

If everything went well, you should see something like this on the console

Server Running in Port xxxxDatabase Connected

In this point we are more than ready to make the tests!!

MySQL ⚙️

Before make test check the file with name createCustomerDB.sql en run the commands in Workbench o console

Endpoints ⚙️

For test in my case I use POSTMAN

Main URL: localhost:xxxx/customers

This request will return the complete list of customers.
GET method: localhost:xxxx/customers

The answer will be something like:

[    {        "id": 1,        "first_name": "John",        "last_name": "Doe",        "email": "[email protected]",        "age": 22    },    {        "id": 2,        "first_name": "John",        "last_name": "Doe",        "email": "[email protected]",        "age": 25    }]

This request will return only 1 customer.
GET by ID method: localhost:xxxx/customers/id/:id

The answer will be something like:

{    "id": 1,    "first_name": "John",    "last_name": "Doe",    "email": "[email protected]",    "age": 22}

This request will create a customer.
POST method: localhost:xxxx/customers/add

The customer information you enter on Postman should look something like:

{    "first_name": "You new customer name",    "last_name": "customer last name",    "email": "[email protected]",    "age": The customer age as number value}

This request will be able to update the data of a specific customer.
PUT method: localhost:xxxx/customers/edit/:id

You can change the data of a specific customer for example:

{    "first_name": "John",    "last_name": "Doe",    "email": "[email protected]",    "age": 40}

to:

{    "first_name": "John",    "last_name": "Doe",    "email": "[email protected]",    "age": 20}

This request will remove a specific customer.
DELETE by ID method: localhost:xxxx/customer/delete/:id

This request will remove all records from the customers table.
DELETE method: localhost:xxxx/customers/deleteCustomers

I hope this example is helpful to you. Feel free to use it or modify it to your liking. I will update it over time

© Federico Berbara 2020