From 7fddc0174dee8a7d2744bdd7b044dd1475c16627 Mon Sep 17 00:00:00 2001 From: Aman254 Date: Thu, 22 Aug 2024 15:43:39 +0530 Subject: [PATCH] Added Solutions for 01 Middlewares 01 and 03 --- week-3/01-middlewares/01-requestcount.js | 18 +++++----- week-3/01-middlewares/02-ratelimitter.js | 20 +++++------ week-3/01-middlewares/03-errorcount.js | 18 +++++----- .../Solutions/01-requestcount.js | 32 +++++++++++++++++ .../01-middlewares/Solutions/03-errorcount.js | 34 +++++++++++++++++++ 5 files changed, 94 insertions(+), 28 deletions(-) create mode 100644 week-3/01-middlewares/Solutions/01-requestcount.js create mode 100644 week-3/01-middlewares/Solutions/03-errorcount.js diff --git a/week-3/01-middlewares/01-requestcount.js b/week-3/01-middlewares/01-requestcount.js index aced495488..12ea37a8b6 100644 --- a/week-3/01-middlewares/01-requestcount.js +++ b/week-3/01-middlewares/01-requestcount.js @@ -1,6 +1,6 @@ -const request = require('supertest'); -const assert = require('assert'); -const express = require('express'); +const request = require("supertest"); +const assert = require("assert"); +const express = require("express"); const app = express(); let requestCount = 0; @@ -10,16 +10,16 @@ let requestCount = 0; // maintain a count of the number of requests made to the server in the global // requestCount variable -app.get('/user', function(req, res) { - res.status(200).json({ name: 'john' }); +app.get("/user", function (req, res) { + res.status(200).json({ name: "john" }); }); -app.post('/user', function(req, res) { - res.status(200).json({ msg: 'created dummy user' }); +app.post("/user", function (req, res) { + res.status(200).json({ msg: "created dummy user" }); }); -app.get('/requestCount', function(req, res) { +app.get("/requestCount", function (req, res) { res.status(200).json({ requestCount }); }); -module.exports = app; \ No newline at end of file +module.exports = app; diff --git a/week-3/01-middlewares/02-ratelimitter.js b/week-3/01-middlewares/02-ratelimitter.js index 3329bb9497..916ca0add3 100644 --- a/week-3/01-middlewares/02-ratelimitter.js +++ b/week-3/01-middlewares/02-ratelimitter.js @@ -1,6 +1,6 @@ -const request = require('supertest'); -const assert = require('assert'); -const express = require('express'); +const request = require("supertest"); +const assert = require("assert"); +const express = require("express"); const app = express(); // You have been given an express server which has a few endpoints. // Your task is to create a global middleware (app.use) which will @@ -13,15 +13,15 @@ const app = express(); let numberOfRequestsForUser = {}; setInterval(() => { - numberOfRequestsForUser = {}; -}, 1000) + numberOfRequestsForUser = {}; +}, 1000); -app.get('/user', function(req, res) { - res.status(200).json({ name: 'john' }); +app.get("/user", function (req, res) { + res.status(200).json({ name: "john" }); }); -app.post('/user', function(req, res) { - res.status(200).json({ msg: 'created dummy user' }); +app.post("/user", function (req, res) { + res.status(200).json({ msg: "created dummy user" }); }); -module.exports = app; \ No newline at end of file +module.exports = app; diff --git a/week-3/01-middlewares/03-errorcount.js b/week-3/01-middlewares/03-errorcount.js index c5374ae6ef..3faffe0a4c 100644 --- a/week-3/01-middlewares/03-errorcount.js +++ b/week-3/01-middlewares/03-errorcount.js @@ -1,6 +1,6 @@ -const request = require('supertest'); -const assert = require('assert'); -const express = require('express'); +const request = require("supertest"); +const assert = require("assert"); +const express = require("express"); const app = express(); let errorCount = 0; @@ -10,17 +10,17 @@ let errorCount = 0; // 1. Ensure that if there is ever an exception, the end user sees a status code of 404 // 2. Maintain the errorCount variable whose value should go up every time there is an exception in any endpoint -app.get('/user', function(req, res) { +app.get("/user", function (req, res) { throw new Error("User not found"); - res.status(200).json({ name: 'john' }); + res.status(200).json({ name: "john" }); }); -app.post('/user', function(req, res) { - res.status(200).json({ msg: 'created dummy user' }); +app.post("/user", function (req, res) { + res.status(200).json({ msg: "created dummy user" }); }); -app.get('/errorCount', function(req, res) { +app.get("/errorCount", function (req, res) { res.status(200).json({ errorCount }); }); -module.exports = app; \ No newline at end of file +module.exports = app; diff --git a/week-3/01-middlewares/Solutions/01-requestcount.js b/week-3/01-middlewares/Solutions/01-requestcount.js new file mode 100644 index 0000000000..36197ca062 --- /dev/null +++ b/week-3/01-middlewares/Solutions/01-requestcount.js @@ -0,0 +1,32 @@ +const request = require("supertest"); +const assert = require("assert"); +const express = require("express"); + +const app = express(); +let requestCount = 0; + +// You have been given an express server which has a few endpoints. +// Your task is to create a global middleware (app.use) which will +// maintain a count of the number of requests made to the server in the global +// requestCount variable + +//Here's the Implementation for the logging the the number on every request made. +app.use((req, res, next) => { + requestCount++; + console.log(requestCount); + next(); +}); + +app.get("/user", function (req, res) { + res.status(200).json({ name: "john" }); +}); + +app.post("/user", function (req, res) { + res.status(200).json({ msg: "created dummy user" }); +}); + +app.get("/requestCount", function (req, res) { + res.status(200).json({ requestCount }); +}); + +module.exports = app; diff --git a/week-3/01-middlewares/Solutions/03-errorcount.js b/week-3/01-middlewares/Solutions/03-errorcount.js new file mode 100644 index 0000000000..b4921a231b --- /dev/null +++ b/week-3/01-middlewares/Solutions/03-errorcount.js @@ -0,0 +1,34 @@ +const request = require("supertest"); +const assert = require("assert"); +const express = require("express"); + +const app = express(); +let errorCount = 0; + +// You have been given an express server which has a few endpoints. +// Your task is to +// 1. Ensure that if there is ever an exception, the end user sees a status code of 404 +// 2. Maintain the errorCount variable whose value should go up every time there is an exception in any endpoint + +app.get("/user", function (req, res) { + throw new Error("User not found"); + // res.status(200).json({ name: "john" }); +}); + +app.post("/user", function (req, res) { + res.status(200).json({ msg: "created dummy user" }); +}); + +app.get("/errorCount", function (req, res) { + res.status(200).json({ errorCount }); +}); + +app.use((err, req, res, next) => { + res.status(404).json({ + status: err.status || 404, + msg: err.message || "An Error Occured.", + }); + errorCount++; +}); + +module.exports = app;