From bbc9e5241633088cfd91d0ccfeaa90cc1f5e9251 Mon Sep 17 00:00:00 2001 From: kloud47 <136368351+kloud47@users.noreply.github.com> Date: Thu, 21 Mar 2024 17:24:51 +0530 Subject: [PATCH] total request calculation --- app.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..90a0459 --- /dev/null +++ b/app.js @@ -0,0 +1,57 @@ +import express from "express"; +import zod from "zod"; +const app = express(); + +const schema = zod.array(zod.number());// array of number validation check +// another example of zod: +const schema2 = zod.object({ + email: zod.string().email(), + password: zod.string().min(8) +}) + +let TotalRequest = 0; + +app.use(express.json()); + +function calculateRequest(req, res, next) { + console.log(TotalRequest); + TotalRequest++; + next(); +} +// middleware for all requests:-----------------------------------------> +// app.use(calculateRequest); + +// request specific middleware: +app.get('/ok', calculateRequest, (req, res) => { + res.status(200).json({ + TotalRequest: TotalRequest + }); +}); + +app.post('/arr', calculateRequest, (req, res) => { + const array = req.body.ans; + // const ansCheck = schema.Parse(array); + const ansCheck = schema.safeParse(array); + const len = ansCheck.data.length; + if (!ansCheck.success) { + res.status(400).send("input is invalid"); + } + res.status(200).send("you have array length " + len); +}); + +// passing params to middleware: +const greet = (greet) => { + return (req, res) => { + res.send(greet + " to you"); + } +} +app.get('/greet', greet("Hi")); + +// global catches:-------------------------------------------> +app.use((err, req, res, next) => { + res.status(400).json({ + msg: "Sorry something is wrong with our server." + }); +}); + +app.listen('5000'); \ No newline at end of file