This is a command-line interface (CLI) application for tracking expenses. It interacts with an API to fetch, add, update, and delete expenses.
Before running the application, make sure you have Node.js and npm installed. Then, install the required dependencies:
npm installCreate a local MySQL database first
CREATE DATABASE expenses;
USE expenses;
CREATE TABLE expenses (
id INT NOT NULL AUTO_INCREMENT,
description VARCHAR(255) NOT NULL,
amount INT NOT NULL,
PRIMARY KEY (id)
);
change the password section in server.js to your mysql password
// Create a MySQL connection pool
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'passwordplaceholder',
database: 'expenses'
});Run the application with the following command:
node server.js // to run the localhost server
node client.js // to run the programThe application will present a menu with the following options:
Get all expenses
Get average expenses
Add an expense
Update an expense
Delete an expense
This function fetches all expenses from the API and logs them to the console. Each expense is logged with its ID, description, amount, and date.
This function fetches the average expenses from the API and logs them to the console. The averages are calculated by the API.
This function adds a new expense to the database.
This function updates an existing expense in the database.
This function deletes an existing expense from the database.
express: A web application framework for Node.js, used to handle HTTP requests and responses.
mysql2/promise: A MySQL client for Node.js with Promise support. It's used to connect to the MySQL database and perform queries.
moment: A JavaScript library for parsing, validating, manipulating, and displaying dates and times. It's used to handle date and time data.
prompt-sync: Used to synchronously get user input in the console.
node-fetch: Used to make HTTP requests to the API.
This API allows you to manage expenses.
Returns a list of all expenses.
Response:
[
{
"id": 1,
"description": "Ayam Goreng",
"amount": 50000,
"date": "2023-10-31T13:21:14.000Z"
},
]
Returns a list of expenses grouped by date, with the total and average amount for each date.
Response:
[
{
"date": "2023-10-31",
"total_amount": 100000,
"average_amount": 50000
},
// ...
]Adds a new expense.
Request body:
{
"description": "Nasi Uduk",
"amount": 15000
}
Response:
{
"id": 1,
"description": "Nasi Uduk",
"amount": 15000,
"date": "2023-10-31T13:21:14.000Z"
}Updates an existing expense.
Request body:
{
"description": "Nasi Uduk",
"amount": 12000
}
Response:
{
"id": 1,
"description": "Nasi Uduk",
"amount": 12000,
"date": "2023-10-31T13:21:14.000Z"
}Deletes an existing expense.
Response:
{
"id": 1
}