From a9ee266e34ba1323ab869b5f18f6cf5163dd73f3 Mon Sep 17 00:00:00 2001 From: Manish Date: Tue, 28 Mar 2023 02:25:02 +1100 Subject: [PATCH] Impl: date api --- index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 27eb0b6..4a23641 100644 --- a/index.js +++ b/index.js @@ -22,8 +22,19 @@ app.get("/", function (req, res) { }); // your first API endpoint... -app.get("/api/hello", function (req, res) { - res.json({ greeting: "hello API" }); +app.get("/api/:date?", (req, res) => { + if (req.params.date) { + if (Number(req.params.date)) { + const date = new Date(Number(req.params.date)); + res.json({ unix: date.getTime(), utc: date.toUTCString() }); + } else { + const date = new Date(req.params.date); + res.json({ unix: date.getTime(), utc: date.toUTCString() }); + } + } else { + const now = new Date(); + res.json({ unix: now.getTime(), utc: now.toUTCString() }); + } }); // listen for requests :)