A simple FizzBuzz API built with Node.js and Express, with a lightweight monitoring solution that tracks CPU and memory usage. The application logs warnings when CPU usage exceeds 90% for 60 seconds or when memory usage is high.
- FizzBuzz API: Accepts a range and returns a FizzBuzz sequence.
- CPU Monitoring: Logs a warning if CPU usage exceeds 90% for a minute.
- Memory Monitoring: Logs a warning if memory usage exceeds 80%.
- Logging: Alerts are logged to
cpu-usage.logwhen the threshold is exceeded.
Make sure you have the following installed on your local machine:
Clone the repository to your local machine:
git clone https://github.com/your-username/fizzbuzz-app.git
cd fizzbuzz-appAfter cloning the repository, install the required dependencies:
npm installThis will install all the dependencies specified in package.json, including the necessary packages for the FizzBuzz API, monitoring, and logging.
To start the application in development mode, run:
npm run devThis will start the server using nodemon, which will automatically restart the server when code changes are detected.
By default, the server runs on port 3000. You can visit the API at:
http://localhost:3000- URL:
POST /fizzbuzz - Body:
{
"fizz_nb": 3,
"buzz_nb": 5,
"start": 1,
"end": 15
}- Response:
{
"result": [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz"]
}
The application monitors the CPU usage. If CPU usage exceeds 90% for 60 seconds, a warning will be logged:
If CPU usage exceeds 90% for 60 seconds.
You can configure the threshold and timing in the app.js file, line 11:
const CPU_THRESHOLD = 90;
const ALERT_DURATION = 60;All logs will be stored in the alerts.log file located in the root directory.
You can open this file to see the logged warnings when CPU usage exceeds the threshold.
To run tests for the application:
Run the tests using:
npm testThe tests will verify the FizzBuzz logic.
If you'd like to containerize the application using Docker, you can follow these steps:
First, make sure your project includes a Dockerfile. Here’s a simple Dockerfile for this Node.js app:
# Dockerfile
FROM node:16
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Then build and run the Docker container:
docker build -t fizzbuzz-app .
docker run -p 3000:3000 fizzbuzz-appNow, your app will be available at http://localhost:3000 in your browser.