Skip to content

himanshudhaka17/SIT737-P1.4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calculator Microservice Using Node.js & Express Overview This project involves developing a RESTful API microservice capable of executing fundamental mathematical operations, including addition, subtraction, multiplication, and division. It is built using Node.js and Express.js and employs Winston for logging purposes. Project Setup

  1. Create the Project Directory – Begin by setting up a new folder for the project.
  2. Initialize Node.js – Run npm init -y to generate a package.json file.
  3. Install Express – Install the Express.js framework using npm install express.

Importing Dependencies The project requires certain modules, including Express for handling HTTP requests and Winston for logging.

Configuring Winston Logger • Logs all incoming requests and errors. • Implements middleware to log requests before processing them.

Input Validation • Ensures that the inputs provided to the API are valid numbers. • Prevents errors by checking for missing or incorrect data.

API Implementation • Validates user inputs before performing operations. • Logs both successful transactions and errors.

Handling Errors (Division by Zero) • If division by zero occurs, an error is logged appropriately to prevent system crashes.

Logging System • Stores combined logs for all requests. • logs/error.log is dedicated to tracking errors.

Monitoring Logs in Real-Time To observe logs as they are recorded, use the command: Get-Content logs/combined.log -Wait Setting Up Git and Pushing to GitHub

  1. Initialize Git with git init.
  2. Add files to the repository: git add ..
  3. Commit the changes: git commit -m "Initial commit".
  4. Link to a GitHub repository using: https://github.com/himanshudhaka17/SIT737-P1.4/tree/main
  5. Push the code to GitHub: git branch -M main
    git push -u origin main

Starting the Server Launch the application using: node server.js GitHub Repository: https://github.com/himanshudhaka17/SIT737-P1.4/tree/main vailable API Endpoints The following endpoints can be accessed for mathematical operations: • Addition: http://localhost:3000/add?num1=100&num2=20

• Subtraction: http://localhost:3000/subtract?num1=100&num2=20

• Multiplication: http://localhost:3000/multiply?num1=-10&num2=-20

• Division: http://localhost:3000/divide?num1=-10&num2=-20

Conclusion: This project creates a RESTful API using Node.js and Express.js to perform basic math operations. It includes input validation, error handling, and logging with Winston. The system is version-controlled with Git and hosted on GitHub, providing a solid foundation for building scalable microservices.

image

for Credit task:

Extended Mathematical Functions in the Calculator Service The calculator microservice has been upgraded with new mathematical capabilities, expanding its utility beyond basic arithmetic. Three advanced operations have been added to provide users with more sophisticated calculation options. Advanced Mathematical Features Power Function This operation determines the result of raising one number to the power of another. It requires two inputs: the base value and the exponent. The calculation follows the form base^exponent, providing essential functionality for scientific research and financial modeling tasks. Root Extraction This feature calculates the square root of a provided number. The function accepts a single numeric input and produces its principal square root. The system validates inputs to prevent mathematically impossible calculations (such as roots of negative numbers) and returns appropriate error notifications when necessary. Remainder Calculation This operation determines what remains after division between two numbers. It takes two parameters: the number being divided and the divisor. This function has practical applications in various computational fields including security algorithms and cyclical calculations. The implementation includes safeguards against zero-divisor errors. These functional additions significantly enhance the calculator service's capabilities, transforming it into a more versatile computational tool suitable for a wider range of mathematical applications. Error Handling: Pushing the Changes to GitHub: git add . git commit -m "Enhanced server.js with new arithmetic functions" git push origin main Building Robust Microservices: Effective Failure Management Techniques Distributed systems based on microservices provide significant benefits in terms of development agility and scalability, but they also create complex failure scenarios. To build resilient systems, engineers employ several key patterns that prevent isolated issues from affecting the entire application ecosystem. Key Resilience Patterns Service Circuit Protection This mechanism monitors service health and automatically stops sending requests to failing components. It functions like an electrical circuit breaker, "opening" when error thresholds are exceeded to prevent overtaxing struggling services. After a cooling period, it tests the service with limited traffic before fully restoring operations. Tools like Resilience4J implement this pattern effectively. Progressive Retry Approaches When temporary disruptions occur, automatic reattempts of failed operations can often succeed. Implementing increasing delays between retry attempts (exponential backoff) prevents overwhelming the target service. This approach is particularly valuable in cloud environments where network instability is common. Graceful Degradation Strategies When primary services are unavailable, systems can deliver alternative responses rather than complete failure. This might involve serving stored data, simplified functionality, or status notifications. Users experience continuity of service, even if with reduced capabilities. System Stability Benefits Properly implemented error handling significantly improves overall system health. Protection mechanisms isolate problems before they spread across services. Smart retry logic handles temporary glitches automatically. Degradation options maintain user experience during partial outages. However, these techniques require careful configuration. Overly aggressive retries can overwhelm systems, while poorly designed fallbacks might provide misleading information. Implementation Considerations Modern frameworks provide ready-to-use implementations of these patterns. The ideal approach combines multiple techniques based on service criticality and failure characteristics. This creates systems that remain functional under challenging conditions, enhancing reliability and user satisfaction.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors