In week 7, a Financial Tracking app was already created. Now in week 8, we are trying to build a simple REST API server based on the Financial Tracking, so user can perform basic CRUD operations on the provided data.
To create this project, we want to install depedencies that we need for this project. First, we need to create Node.js project by using npm init in terminal. After that, install the dependecies, such as :
- express.js and dotenv package:
npm i express dotenv
- Install Typescript, @types/express, and @types/node package:
npm i -D typescript @types/express @types/node
- Install nodemon as devDependencies:
npm i -D nodemon
- Install concurrently as devDependencies:
npm i -D concurrently
- Install body-parser:
npm i body-parser
- Install @types/body-parser as devDependencies:
npm i -D @types/body-parser
- Update the script in package.json:
After all dependencies already installed to the project, generating tsconfig.json using npx tsc --init , then change outDir in tsconfig.json as: "outDir": "./dist"
After the REST API server already build, it's time to deploy it with fly.io. To use fly.io, we need to install it to our PC before we can use it.
- Run the PowerShell, then use this command to install flyctl:
pwsh -Command "iwr https://fly.io/install.ps1 -useb | iex"
- Sign in / Sign up to fly.io.
- We need to authorize our login with PowerShell, so use this command:
flyctl auth login
- Change the PowerShell directory to the project directory.
- Launch the app using this command:
flyctl launch
- Here, we can enter the name of the app and then select a region to deploy in.
- Finally, flyctl will ask you if you would like to deploy. Use this command:
flyctl deploy
- After deploy is success, we can use command flyctl status to show the app basic detail.
- App successfully deployed.
To test the APIs, we need to install Postman so we can perform basic CRUD (Create, Read, Update, Delete) operations. The financial tracking API that has been created has endpoints with the following HTTP methods:
- GET/expenses : Get all expenses data.
- GET/expenses/:id : Get expense data by using ID.
- POST/expenses : Create new expense data.
- PUT/expenses/:id : Updates an existing data with the given ID.
- PATCH/expenses/:id : Partially update an existing data with the given ID.
- DELETE/expenses/:id : Delete an existing data with the given ID.
Thank you ๐