From 8ea72518d0c5bce8d882708c1dcf9cac310ea545 Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Fri, 7 Oct 2022 15:40:33 +0200 Subject: [PATCH] refactor: use findOne instead of find --- index.ts | 12 ++++++------ util/consolelogTime.ts | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 util/consolelogTime.ts diff --git a/index.ts b/index.ts index 2b05903..71d00b3 100644 --- a/index.ts +++ b/index.ts @@ -6,6 +6,7 @@ dotenv.config() import sernTime from "./schemas/sern-time.js" import bodyParser from "body-parser" import rateLimit from "express-rate-limit" +import { consolelogTime } from "./util/consolelogTime.js" app.use(bodyParser.json()) app.disable("x-powered-by") const limiter = rateLimit({ @@ -19,7 +20,7 @@ app.use(limiter) const englishRegex = /^[A-Za-z0-9]*$/ await mongoose.connect(`${process.env.MONGODB}`).then(() => { - console.log("Connected to MongoDB!") + consolelogTime(`Connected to MongoDB!`) }) app.post("/sern/newTime", async (req, res, next) => { @@ -64,20 +65,19 @@ app.post("/sern/newTime", async (req, res, next) => { app.get("/sern/availableTime", async (req, res, next) => { let get = (await sernTime.find()) as any - get = get.map((data) => data.name) + get = get.map(data => data.name) res.send(get) }) app.get("/sern/getTime", async (req, res, next) => { if (req.query.name) { - let get = (await sernTime.find({ name: req.query.name })) as any - get = get.map((data) => data.timezone) - res.send(get) + const get = await sernTime.findOne({ name: req.query.name }) + res.send(get!.timezone) } else { res.json({ "error": "Option name not provided." }) } }) app.listen(7272, () => { - console.log(`listening`) + consolelogTime(`listening`) }) diff --git a/util/consolelogTime.ts b/util/consolelogTime.ts new file mode 100644 index 0000000..a92cf23 --- /dev/null +++ b/util/consolelogTime.ts @@ -0,0 +1,17 @@ + + +export async function consolelogTime(message: string) { + let unix_timestamp = Date.now() + // Create a new JavaScript Date object based on the timestamp + // multiplied by 1000 so that the argument is in milliseconds, not seconds. + var date = new Date(unix_timestamp * 1000); + // Hours part from the timestamp + var hours = date.getHours(); + // Minutes part from the timestamp + var minutes = "0" + date.getMinutes(); + // Seconds part from the timestamp + var seconds = "0" + date.getSeconds(); + const convertedTime = hours + ':' + minutes.substring(-2) + ':' + seconds.substring(-2); + + return console.log(`[${convertedTime}] ${message}`); +} \ No newline at end of file