FlavioAro / rest-api-nodejs-mysql-crud-v2

RestAPI template using NodeJS + Express + MySQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rest-api-nodejs-mysql-crud-v2

RestAPI template using NodeJS + Express + MySQL

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.

Installation

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!!

MySQL

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

Endpoints

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


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

About

RestAPI template using NodeJS + Express + MySQL


Languages

Language:JavaScript 98.7%Language:Shell 1.3%