/simple-restapi-mysql

Creating Example of Rest API using Express, MySQL, Multer, Express-Validator

Primary LanguageJavaScript

restapi-mysql

Creating Example Rest API using Express, MySQL, Multer.

# Express.js simple rest api with mysql and Multer for uploading image.

This is a simple example of an Express.js rest api with mysql and Multer for uploading image..

## Prerequisites

Before running the application, make sure you have the following installed on your machine:

- [Node.js](https://nodejs.org/)
- [npm](https://www.npmjs.com/) (Node Package Manager)

Getting Started

  1. Clone this repository to your local machine:

    git clone https://github.com/Hafizcode02/restapi-mysql.git
  2. Navigate to the project directory:

    cd restapi-mysql
  3. Install the dependencies:

    npm install

SETTING UP .ENV File

before starting running the application, set env file based on your needs:

PORT=XXXX
DB_HOST=XXX
DB_USERNAME=XXXX
DB_PASSWORD=XXXX
DB_NAME=XXXX

SQL File is in folder, just import it..

Running the Application

To start the Express.js server and run the file upload example, use the following command:

npm start run-dev

This will start the server on http://localhost:ENV_PORT or in port 8004.

File Upload

  • The file upload route is configured at /upload.
  • Only specific file types (JPEG, PNG, and PDF) are allowed.

API Endpoint List

[POST] your-site/users
[GET] your-site/users
[GET] your-site/users/:id
[PATCH] your-site/users/:id
[DELETE] your-site/users/:id
[POST] your-site/upload/

Your Body Data

For POST, PATCH users use raw JSON type
{
    "name" : "Argh Satrio",
    "email" : "landa@gmail.com",
    "address": "World"
}

For POST images/files, use form-data type. 

photo : abcd.jpg