Repositori ini berisi REST API sederhana yang dibangun dengan Node.js dan MySQL. API ini mendukung operasi CRUD (Create, Read, Update, Delete) dasar untuk tabel users.
- Clone repositori ini:
git clone https://github.com/yourusername/my-api.git cd my-api - Install dependensi yang diperlukan:
npm install
- Atur database MySQL:
- Jalankan server MySQL Anda.
- Buat database bernama testdb dan tabel users dengan menjalankan perintah SQL berikut:
CREATE DATABASE testdb; USE testdb; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) );
- Perbarui detail koneksi MySQL di 'server.js':
const db = mysql.createConnection({ host: 'localhost', user: 'your_mysql_username', // ganti dengan username MySQL Anda password: 'your_mysql_password', // ganti dengan password MySQL Anda database: 'testdb' });
Jalankan server dengan perintah:
bash node server.js
Server akan berjalan di http://localhost:3000.
- Deskripsi: Mendapatkan semua item.
- Request:
GET /api/users - Response:
[ { "id": 1, "name": "John Doe", "email": "john@example.com" }, ]
- Deskripsi: Mendapatkan pengguna berdasarkan ID.
- Request:
GET /api/users/:id - Response:
{ "id": 1, "name": "John Doe", "email": "john@example.com" }
- Deskripsi: Menambahkan pengguna baru.
- Request:
POST /api/users- Headers:
Content-Type: application/json - Body:
{ "name": "Jane Doe", "email": "jane@example.com" }
- Headers:
- Response:
{ "id": 2, "name": "Jane Doe", "email": "jane@example.com" }
- Deskripsi: Menghapus pengguna berdasarkan ID.
- Request:
DELETE /api/users/:id - Response:
{ "affectedRows": 1 }
- Deskripsi: Memperbarui pengguna berdasarkan ID.
- Request:
PUT /api/users/:id- Headers:
Content-Type: application/json - Body:
{ "name": "Jane Smith", "email": "jane.smith@example.com" }
- Headers:
- Response:
{ "affectedRows": 1 }
-
GET Semua Users:
- URL:
http://localhost:3000/api/users - Method:
GET
- URL:
-
GET Users Berdasarkan ID:
- URL:
http://localhost:3000/api/users/1 - Method:
GET
- URL:
-
POST Users Baru:
- URL:
http://localhost:3000/api/users - Method:
POST - Headers:
Content-Type: application/json - Body:
{ "name": "User 3", "email": "user3@example.com" }
- URL:
-
DELETE Users Berdasarkan ID:
- URL:
http://localhost:3000/api/users/1 - Method:
DELETE
- URL:
-
PUT Update Users Berdasarkan ID:
- URL:
http://localhost:3000/api/users/1 - Method:
PUT - Headers:
Content-Type: application/json - Body:
{ "name": "User 3 Updated", "email": "user3@example.com" }
- URL:
-
GET Semua Users:
curl -X GET http://localhost:3000/api/users
-
POST Users Baru:
curl -X POST http://localhost:3000/api/users \ -H "Content-Type: application/json" \ -d '{"name": "User 3", "email": "user3@example.com"}'
-
GET Users Berdasarkan ID:
curl -X GET http://localhost:3000/api/users/1
-
DELETE Users Berdasarkan ID:
curl -X DELETE http://localhost:3000/api/users/1
-
PUT Update Users Berdasarkan ID:
curl -X PUT http://localhost:3000/api/users/1 \ -H "Content-Type: application/json" \ -d '{"name": "User 3 Updated", "email": "user3@example.com"}'
Proyek ini dilisensikan di bawah MIT License.