A web application for calculating probabilities using two methods:
- Combined With: P(A)P(B)
- Either: P(A) + P(B) – P(A)P(B)
This project was designed to provide a simple and efficient way to calculate probabilities using common probability formulas. The backend is built with Node.js and Express, ensuring a robust and scalable server-side application. The frontend is built with React for a dynamic user interface, Axios for its simplicity and ease of handling HTTP requests, and Tailwind CSS for its utility-first approach to styling.
- Probability input validation
- Two calculation methods
- Logging of calculations
- Simple, clean UI
Node.js, Express, React, TypeScript, Tailwind CSS, Axios, Jest, Winston.
- Node.js (v14+ recommended)
- npm
-
Navigate to the backend directory:
cd backend
-
Install dependencies:
npm install
-
Create a
.env
file in the backend directory and add the following environment variables:PORT=5000
-
Start the server:
npm run dev
The server will start on
http://localhost:5000
.
-
Navigate to the frontend directory:
cd frontend
-
Install dependencies:
npm install
-
Start the frontend application:
npm start
The frontend will start on
http://localhost:3000
.
- Start the backend server (will run on http://localhost:5000)
- Start the frontend application (will run on http://localhost:3000)
You can use Postman to test the API endpoints. For example:
-
Open Postman and create a new
POST
request tohttp://localhost:5000/api/calculator/combined
. -
Set the request body to JSON and include the following payload:
{ "a": 0.5, "b": 0.5 }
-
Send the request and you should receive a response with the combined probability result.
- server.ts: Main server file.
- routes/calculatorRoutes.ts: Defines API routes.
- controllers/calculatorController.ts: Handles calculator operations.
- services/calculatorService.ts: Core probability calculation logic.
- public: Static files directory.
- App.tsx: Main React component.
- components: Directory containing React components.
- ProbabilityCalculator.tsx: Component for the probability calculator.
- styles: Directory containing CSS files.
- index.css: Main CSS file.
- setupTests.ts: Setup file for Jest and testing-library configurations.
Express Framework: Chosen for its simplicity and flexibility in building server-side applications.
React: Component-based architecture for a dynamic UI.
Modular Structure: The project is organized into routes, controllers, and services to promote separation of concerns and maintainability.
Helmet: Used for setting various HTTP headers to enhance security.
CORS: Configured to allow requests from the frontend running on http://localhost:3000
.
Body-Parser: Middleware to parse incoming request bodies in JSON format.
Environment Variables: Managed using dotenv
to keep configuration separate from the codebase.
Tailwind CSS: For efficient and customizable styling using utility classes.
Axios: For making HTTP requests with a simple and promise-based API.
Jest: JavaScript testing framework for comprehensive and efficient testing.
Winston: For versatile and configurable logging.
- Add more probability calculation methods.
- Implement user authentication and authorization.
- Improve error handling and logging.
- Enhance the frontend UI/UX.
This project provides a foundational setup for a probability calculator application. The modular structure and use of popular middleware ensure that the application is secure, maintainable, and scalable. The combination of Node.js, Express, and React provides a robust and dynamic platform for building web applications.