This project implements a simple Flask-based microservice that generates and returns random motivational quotes. The microservice is designed to be used by other applications via a RESTful API.
The goal of this README is to guide how to request and receive data from this microservice programmatically.
To request a random motivational quote from the Flask microservice, your application must send an HTTP GET request to the /api/quote endpoint.
If your backend is built using Node.js with the Axios library, here's how to make the request:
-
Install Axios (if you haven't already):
npm install axios
-
Send a GET request to the Flask microservice:
const axios = require('axios'); async function getMotivationalQuote() { try { // Send GET request to Flask microservice const response = await axios.get('http://127.0.0.1:5000/api/quote'); console.log(response.data.quote); // Log the received quote } catch (error) { console.error('Error fetching quote:', error); } } getMotivationalQuote(); // Example function call to fetch a quote
- GET
http://127.0.0.1:5000/api/quote
This URL assumes the Flask microservice is running locally. Replace 127.0.0.1:5000 with your deployed server’s URL if it’s hosted remotely.
The response from the Flask microservice will be a JSON object containing the quote.
{
"quote": "Believe you can and you're halfway there."
}Once the quote is fetched, you can process and forward it to the frontend (if you're using a backend to fetch quotes for a frontend):
const express = require('express');
const axios = require('axios');
const app = express();
// Route to fetch quote from Flask microservice
app.get('/api/quote', async (req, res) => {
try {
const response = await axios.get('http://127.0.0.1:5000/api/quote');
res.json(response.data); // Send quote back to frontend
} catch (error) {
res.status(500).json({ message: 'Error fetching quote', error: error.message });
}
});
const PORT = 5001;
app.listen(PORT, () => {
console.log(`Backend is running on port ${PORT}`);
});In a React application, you can use Axios to fetch the quote and display it:
import axios from 'axios';
import React, { useState, useEffect } from 'react';
const MotivationalQuote = () => {
const [quote, setQuote] = useState('');
useEffect(() => {
async function fetchQuote() {
try {
const response = await axios.get('/api/quote');
setQuote(response.data.quote); // Set the quote to display
} catch (error) {
console.error('Error fetching quote:', error);
}
}
fetchQuote(); // Fetch quote on component mount
}, []);
return (
<div>
<h2>Motivational Quote:</h2>
<p>{quote}</p> {/* Display the quote */}
</div>
);
};
export default MotivationalQuote;Below is a UML Sequence Diagram that illustrates the process of requesting and receiving data between the frontend, backend, and Flask microservice.
+----------------+ +-----------------+ +--------------------+
| React Frontend | | Express Backend | | Flask Microservice |
+----------------+ +-----------------+ +--------------------+
| | |
|-----(1) Request Quote------>| |
| | |
| |----(2) GET /api/quote-------->|
| | |
| |<---(3) Return Quote-----------|
| | |
|<----(4) Response------------| |
| Display Quote | |
+---- -----------+ +-----------------+ +--------------------+
- The React frontend sends a GET request to the Express backend at
/api/quote. - The Express backend makes a GET request to the Flask microservice at
http://127.0.0.1:5000/api/quoteto fetch the motivational quote. - The Flask microservice responds with a JSON object containing the quote.
- The Express backend forwards the quote back to the React frontend.
- The React frontend displays the quote to the user.
-
Running the Flask Microservice:
- Ensure you have Python and Flask installed. If not, install Flask using
pip install Flask. - Run the Flask application with the command:
python app.py
- The Flask microservice will be available at
http://127.0.0.1:5000.
- Ensure you have Python and Flask installed. If not, install Flask using
-
Running the Express Backend:
- Ensure you have Node.js and Axios installed. If not, install Axios via:
npm install axios
- Start the backend server with:
node server.js
- The Express server will be running on
http://127.0.0.1:5001.
- Ensure you have Node.js and Axios installed. If not, install Axios via:
End of README