UNPKG

sahaj-therapy

Version:

An Express middleware for SVF and AVN regenerative therapy evaluation.

113 lines (98 loc) 3.5 kB
const express = require('express'); const cors = require('cors'); function createHealthTipsGenerator() { const router = express.Router(); const tokenRequestCounts = new Map(); // Middleware setup router.use(express.json()); router.use(cors()); // Health tips database const healthTips = { sedentary: [ "Take a 5-minute break every hour for stretching", "Try desk exercises like shoulder rolls and ankle rotations", "Practice good posture while sitting", "Set up an ergonomic workspace", "Take short walking breaks between meetings" ], yoga: [ "Start your day with 10 minutes of Sun Salutations", "Practice mindful breathing during your yoga session", "Include balance poses in your routine", "End your day with relaxing yoga poses", "Combine meditation with your yoga practice" ], meditation: [ "Start with 5 minutes of morning meditation", "Practice mindful eating during meals", "Take mindful breathing breaks", "Try body scan meditation before sleep", "Use walking meditation during breaks" ], vegetarian: [ "Include protein-rich legumes in your meals", "Ensure adequate B12 intake through fortified foods", "Combine different plant proteins for complete nutrition", "Include calcium-rich vegetables in your diet", "Try new seasonal vegetables each week" ] }; router.post('/', (req, res) => { const { age, lifestyle, preferences, dietaryRestrictions, token } = req.body; // Rate limiting const requestCount = tokenRequestCounts.get(token) || 0; if (requestCount >= 10) { return res.status(429).json({ error: 'Too many requests. Please try again later.' }); } tokenRequestCounts.set(token, requestCount + 1); try { // Generate personalized tip let relevantTips = []; // Add lifestyle-specific tips if (lifestyle && healthTips[lifestyle]) { relevantTips = relevantTips.concat(healthTips[lifestyle]); } // Add preference-specific tips if (preferences) { preferences.forEach(pref => { if (healthTips[pref]) { relevantTips = relevantTips.concat(healthTips[pref]); } }); } // Add dietary-specific tips if (dietaryRestrictions) { dietaryRestrictions.forEach(diet => { if (healthTips[diet]) { relevantTips = relevantTips.concat(healthTips[diet]); } }); } // If no specific tips found, provide general tips if (relevantTips.length === 0) { relevantTips = [ "Stay hydrated by drinking water throughout the day", "Aim for 7-9 hours of quality sleep", "Include fruits and vegetables in every meal", "Take regular breaks from screen time", "Practice deep breathing exercises" ]; } // Select a random tip const tip = relevantTips[Math.floor(Math.random() * relevantTips.length)]; res.json({ tip, timestamp: new Date().toISOString() }); } catch (error) { console.error('Error:', error); res.status(500).json({ error: 'An error occurred while generating health tip.' }); } }); return router; } module.exports = createHealthTipsGenerator;