Created
November 28, 2025 11:04
-
-
Save suhailroushan13/4a2a189ae4850c209edf5ad572f8a171 to your computer and use it in GitHub Desktop.
Learn API's for Students
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import express from "express"; | |
| const app = express(); | |
| app.use(express.json()); | |
| // 1️⃣ Add two numbers | |
| app.post("/add", (req, res) => { | |
| const { a, b } = req.body; | |
| res.json({ result: a + b }); | |
| }); | |
| // 2️⃣ Multiply numbers | |
| app.post("/multiply", (req, res) => { | |
| const { x, y } = req.body; | |
| res.json({ result: x * y }); | |
| }); | |
| // 3️⃣ Compare numbers | |
| app.post("/compare", (req, res) => { | |
| const { x, y } = req.body; | |
| if (x > y) return res.json({ result: "x is greater" }); | |
| if (y > x) return res.json({ result: "y is greater" }); | |
| res.json({ result: "both are equal" }); | |
| }); | |
| // 4️⃣ Even or Odd | |
| app.post("/even-odd", (req, res) => { | |
| const { num } = req.body; | |
| res.json({ result: num % 2 === 0 ? "even" : "odd" }); | |
| }); | |
| // 5️⃣ Reverse String | |
| app.post("/reverse", (req, res) => { | |
| const { text } = req.body; | |
| res.json({ result: text.split("").reverse().join("") }); | |
| }); | |
| // 6️⃣ Palindrome Check | |
| app.post("/palindrome", (req, res) => { | |
| const { text } = req.body; | |
| const reversed = text.split("").reverse().join(""); | |
| res.json({ result: text === reversed ? "palindrome" : "not palindrome" }); | |
| }); | |
| // 7️⃣ Vowel Count | |
| app.post("/vowels", (req, res) => { | |
| const { text } = req.body; | |
| const count = text.match(/[aeiouAEIOU]/g)?.length || 0; | |
| res.json({ result: count }); | |
| }); | |
| // 8️⃣ Age Eligibility | |
| app.post("/eligibility", (req, res) => { | |
| const { age } = req.body; | |
| res.json({ result: age >= 18 ? "eligible" : "not eligible" }); | |
| }); | |
| // 9️⃣ Max from Array | |
| app.post("/max", (req, res) => { | |
| const { numbers } = req.body; | |
| res.json({ result: Math.max(...numbers) }); | |
| }); | |
| // 🔟 Celsius → Fahrenheit | |
| app.post("/temperature", (req, res) => { | |
| const { c } = req.body; | |
| const f = (c * 9) / 5 + 32; | |
| res.json({ result: f }); | |
| }); | |
| // ============================= | |
| // Bonus +10 APIs | |
| // ============================= | |
| // 1️⃣1️⃣ Age Calculator | |
| app.post("/age", (req, res) => { | |
| const { birthYear } = req.body; | |
| const currentYear = new Date().getFullYear(); | |
| res.json({ result: currentYear - birthYear }); | |
| }); | |
| // 1️⃣2️⃣ Square of Number | |
| app.post("/square", (req, res) => { | |
| const { num } = req.body; | |
| res.json({ result: num * num }); | |
| }); | |
| // 1️⃣3️⃣ Simple Interest | |
| app.post("/simple-interest", (req, res) => { | |
| const { p, r, t } = req.body; | |
| const si = (p * r * t) / 100; | |
| res.json({ result: si }); | |
| }); | |
| // 1️⃣4️⃣ Area of Circle | |
| app.post("/circle-area", (req, res) => { | |
| const { radius } = req.body; | |
| const area = 3.14 * radius * radius; | |
| res.json({ result: area }); | |
| }); | |
| // 1️⃣5️⃣ Grade Calculator | |
| app.post("/grade", (req, res) => { | |
| const { marks } = req.body; | |
| let grade = ""; | |
| if (marks >= 90) grade = "A"; | |
| else if (marks >= 75) grade = "B"; | |
| else if (marks >= 50) grade = "C"; | |
| else grade = "Fail"; | |
| res.json({ result: grade }); | |
| }); | |
| // 1️⃣6️⃣ Convert to Uppercase | |
| app.post("/upper", (req, res) => { | |
| const { text } = req.body; | |
| res.json({ result: text.toUpperCase() }); | |
| }); | |
| // 1️⃣7️⃣ Convert to Lowercase | |
| app.post("/lower", (req, res) => { | |
| const { text } = req.body; | |
| res.json({ result: text.toLowerCase() }); | |
| }); | |
| // 1️⃣8️⃣ Minimum from Array | |
| app.post("/min", (req, res) => { | |
| const { numbers } = req.body; | |
| res.json({ result: Math.min(...numbers) }); | |
| }); | |
| // 1️⃣9️⃣ Positive or Negative | |
| app.post("/sign", (req, res) => { | |
| const { num } = req.body; | |
| res.json({ result: num > 0 ? "positive" : num < 0 ? "negative" : "zero" }); | |
| }); | |
| // 2️⃣0️⃣ String Length | |
| app.post("/length", (req, res) => { | |
| const { text } = req.body; | |
| res.json({ result: text.length }); | |
| }); | |
| // 🚀 Server Start | |
| app.listen(5000, () => { | |
| console.log("Server running at http://localhost:5000"); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment