विषय पर बढ़ें

Node.js उदाहरण – हेल्थ फिटनेस एपीआई

सर्वर-साइड अनुप्रयोगों में हेल्थ फिटनेस एपीआई तक पहुंचने के लिए Node.js का उपयोग करें।


1. अंतर्निहित https मॉड्यूल का उपयोग करना

GET अनुरोध का उदाहरण:

````javascript const https = require("https");

const apiKey = "YOUR_API_KEY"; const url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

https.get(url, { headers: { "X-API-Key": apiKey } }, (res) => { let data = ""; res.on("data", (chunk) => { data += chunk; }); res.on("end", () => { console.log(JSON.parse(data)); }); }).on("error", (err) => { console.error("Error:", err); }); `` axios

POST request example:

```javascript const https = require("https");

const payload = JSON.stringify({ height: 178, weight: 82, units: "metric" }); const options = { hostname: "api.hefitapi.com", path: "/api/v1/bmi/post", method: "POST", headers: { "X-API-Key": "YOUR_API_KEY", "Content-Type": "application/json", "Content-Length": Buffer.byteLength(payload) } };

const req = https.request(options, (res) => { let data = ""; res.on("data", (chunk) => { data += chunk; }); res.on("end", () => { console.log(JSON.parse(data)); }); });

req.on("error", (err) => { console.error("Error:", err); }); req.write(payload); req.end(); ```


2. Using axios

Install node-fetch

Install node-fetch:

bash npm install node-fetch

GET request example:

```javascript const fetch = require("node-fetch");

const apiKey = "YOUR_API_KEY"; const url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

fetch(url, { headers: { "X-API-Key": apiKey } }) .then(res => res.json()) .then(data => console.log(data)) .catch(err => console.error("Error:", err)); ```

POST request example with async/await:

```javascript const fetch = require("node-fetch");

const apiKey = "YOUR_API_KEY"; const url = "https://api.hefitapi.com/api/v1/bmi/post";

const payload = { height: 178, weight: 82, units: "metric" };

async function postBMI() { try { const response = await fetch(url, { method: "POST", headers: { "X-API-Key": apiKey, "Content-Type": "application/json" }, body: JSON.stringify(payload) }); const data = await response.json(); console.log(data); } catch (err) { console.error("Error:", err); } }

postBMI(); ```


4. Notes & Best Practices

  • Node.js allows server-side requests without CORS issues.
  • Use async/await or Promises for cleaner control flow.
  • Protect your API key; never expose it in client-side code.
  • Responses include _enterprise मेटाडेटा का उपयोग निगरानी के लिए, यदि लागू हो।

आज ही अपने Python ऐप्स में हेल्थ फिटनेस एपीआई को एकीकृत करना शुरू करें!


हेल्थ फिटनेस एपीआई आधुनिक डिजिटल स्वास्थ्य बुनियादी ढांचे को शक्ति प्रदान करना