Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Satyam #1162

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Satyam #1162

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Week-2

middlewares and jwt assignment completed.
3 changes: 3 additions & 0 deletions week-3/01-middlewares/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
package.json
package-lock.json
week-3/01-middlewares/coverage/
24 changes: 15 additions & 9 deletions week-3/01-middlewares/01-requestcount.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,17 +9,23 @@ let requestCount = 0;
// 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
app.use((req, res, next) => {
requestCount = requestCount + 1;
next();
});

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;
// app.listen(3000);

module.exports = app;
36 changes: 26 additions & 10 deletions week-3/01-middlewares/02-ratelimitter.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,15 +13,31 @@ const app = express();

let numberOfRequestsForUser = {};
setInterval(() => {
numberOfRequestsForUser = {};
}, 1000)
numberOfRequestsForUser = {};
}, 1000);

app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
app.use(function (req, res, next) {
const userId = req.headers["user-id"];

if (!numberOfRequestsForUser[userId]) {
numberOfRequestsForUser[userId] = 1;
return next();
}
if (numberOfRequestsForUser[userId]) {
numberOfRequestsForUser[userId]++;
if (numberOfRequestsForUser[userId] > 5) {
res.status(404).send("Too Many Attemps");
}
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.post("/user", function (req, res) {
res.status(200).json({ msg: "created dummy user" });
});

module.exports = app;
module.exports = app;
23 changes: 14 additions & 9 deletions week-3/01-middlewares/03-errorcount.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,17 +10,22 @@ 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;
app.use((err, req, res, next) => {
res.status(404).send({});
errorCount++;
});

module.exports = app;
40 changes: 40 additions & 0 deletions week-3/01-middlewares/coverage/clover.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1736877925424" clover="3.2.0">
<project timestamp="1736877925424" name="All files">
<metrics statements="31" coveredstatements="29" conditionals="4" coveredconditionals="3" methods="0" coveredmethods="0" elements="35" coveredelements="32" complexity="0" loc="31" ncloc="31" packages="1" files="1" classes="1"/>
<file name="03-errorcount.js" path="C:\Users\ssaty\OneDrive\Desktop\Cohort2\assignments\week-3\01-middlewares\03-errorcount.js">
<metrics statements="31" coveredstatements="29" conditionals="4" coveredconditionals="3" methods="0" coveredmethods="0"/>
<line num="1" count="1" type="stmt"/>
<line num="2" count="1" type="stmt"/>
<line num="3" count="1" type="stmt"/>
<line num="4" count="1" type="stmt"/>
<line num="5" count="1" type="stmt"/>
<line num="6" count="1" type="stmt"/>
<line num="7" count="1" type="stmt"/>
<line num="8" count="1" type="stmt"/>
<line num="9" count="1" type="stmt"/>
<line num="10" count="1" type="stmt"/>
<line num="11" count="1" type="stmt"/>
<line num="12" count="1" type="stmt"/>
<line num="13" count="1" type="cond" truecount="1" falsecount="0"/>
<line num="14" count="11" type="cond" truecount="0" falsecount="1"/>
<line num="15" count="0" type="stmt"/>
<line num="16" count="1" type="stmt"/>
<line num="17" count="1" type="stmt"/>
<line num="18" count="1" type="stmt"/>
<line num="19" count="0" type="stmt"/>
<line num="20" count="1" type="stmt"/>
<line num="21" count="1" type="stmt"/>
<line num="22" count="1" type="cond" truecount="1" falsecount="0"/>
<line num="23" count="2" type="stmt"/>
<line num="24" count="1" type="stmt"/>
<line num="25" count="1" type="stmt"/>
<line num="26" count="1" type="cond" truecount="1" falsecount="0"/>
<line num="27" count="11" type="stmt"/>
<line num="28" count="11" type="stmt"/>
<line num="29" count="1" type="stmt"/>
<line num="30" count="1" type="stmt"/>
<line num="31" count="1" type="stmt"/>
</file>
</project>
</coverage>
2 changes: 2 additions & 0 deletions week-3/01-middlewares/coverage/coverage-final.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"C:\\Users\\ssaty\\OneDrive\\Desktop\\Cohort2\\assignments\\week-3\\01-middlewares\\03-errorcount.js": {"path":"C:\\Users\\ssaty\\OneDrive\\Desktop\\Cohort2\\assignments\\week-3\\01-middlewares\\03-errorcount.js","all":false,"statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},"1":{"start":{"line":2,"column":0},"end":{"line":2,"column":33}},"2":{"start":{"line":3,"column":0},"end":{"line":3,"column":35}},"3":{"start":{"line":4,"column":0},"end":{"line":4,"column":0}},"4":{"start":{"line":5,"column":0},"end":{"line":5,"column":22}},"5":{"start":{"line":6,"column":0},"end":{"line":6,"column":19}},"6":{"start":{"line":7,"column":0},"end":{"line":7,"column":0}},"7":{"start":{"line":8,"column":0},"end":{"line":8,"column":67}},"8":{"start":{"line":9,"column":0},"end":{"line":9,"column":18}},"9":{"start":{"line":10,"column":0},"end":{"line":10,"column":87}},"10":{"start":{"line":11,"column":0},"end":{"line":11,"column":112}},"11":{"start":{"line":12,"column":0},"end":{"line":12,"column":0}},"12":{"start":{"line":13,"column":0},"end":{"line":13,"column":38}},"13":{"start":{"line":14,"column":0},"end":{"line":14,"column":36}},"14":{"start":{"line":15,"column":0},"end":{"line":15,"column":41}},"15":{"start":{"line":16,"column":0},"end":{"line":16,"column":3}},"16":{"start":{"line":17,"column":0},"end":{"line":17,"column":0}},"17":{"start":{"line":18,"column":0},"end":{"line":18,"column":39}},"18":{"start":{"line":19,"column":0},"end":{"line":19,"column":54}},"19":{"start":{"line":20,"column":0},"end":{"line":20,"column":3}},"20":{"start":{"line":21,"column":0},"end":{"line":21,"column":0}},"21":{"start":{"line":22,"column":0},"end":{"line":22,"column":44}},"22":{"start":{"line":23,"column":0},"end":{"line":23,"column":39}},"23":{"start":{"line":24,"column":0},"end":{"line":24,"column":3}},"24":{"start":{"line":25,"column":0},"end":{"line":25,"column":0}},"25":{"start":{"line":26,"column":0},"end":{"line":26,"column":34}},"26":{"start":{"line":27,"column":0},"end":{"line":27,"column":27}},"27":{"start":{"line":28,"column":0},"end":{"line":28,"column":15}},"28":{"start":{"line":29,"column":0},"end":{"line":29,"column":3}},"29":{"start":{"line":30,"column":0},"end":{"line":30,"column":0}},"30":{"start":{"line":31,"column":0},"end":{"line":31,"column":21}}},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1,"13":11,"14":0,"15":1,"16":1,"17":1,"18":0,"19":1,"20":1,"21":1,"22":2,"23":1,"24":1,"25":1,"26":11,"27":11,"28":1,"29":1,"30":1},"branchMap":{"0":{"type":"branch","line":13,"loc":{"start":{"line":13,"column":17},"end":{"line":16,"column":2}},"locations":[{"start":{"line":13,"column":17},"end":{"line":16,"column":2}}]},"1":{"type":"branch","line":14,"loc":{"start":{"line":14,"column":35},"end":{"line":16,"column":2}},"locations":[{"start":{"line":14,"column":35},"end":{"line":16,"column":2}}]},"2":{"type":"branch","line":22,"loc":{"start":{"line":22,"column":23},"end":{"line":24,"column":2}},"locations":[{"start":{"line":22,"column":23},"end":{"line":24,"column":2}}]},"3":{"type":"branch","line":26,"loc":{"start":{"line":26,"column":8},"end":{"line":29,"column":2}},"locations":[{"start":{"line":26,"column":8},"end":{"line":29,"column":2}}]}},"b":{"0":[11],"1":[0],"2":[2],"3":[11]},"fnMap":{},"f":{}}
}
178 changes: 178 additions & 0 deletions week-3/01-middlewares/coverage/lcov-report/01-requestcount.js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@

<!doctype html>
<html lang="en">

<head>
<title>Code coverage report for 01-requestcount.js</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="prettify.css" />
<link rel="stylesheet" href="base.css" />
<link rel="shortcut icon" type="image/x-icon" href="favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type='text/css'>
.coverage-summary .sorter {
background-image: url(sort-arrow-sprite.png);
}
</style>
</head>

<body>
<div class='wrapper'>
<div class='pad1'>
<h1><a href="index.html">All files</a> 01-requestcount.js</h1>
<div class='clearfix'>

<div class='fl pad1y space-right2'>
<span class="strong">96.77% </span>
<span class="quiet">Statements</span>
<span class='fraction'>30/31</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">100% </span>
<span class="quiet">Branches</span>
<span class='fraction'>3/3</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">100% </span>
<span class="quiet">Functions</span>
<span class='fraction'>0/0</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">96.77% </span>
<span class="quiet">Lines</span>
<span class='fraction'>30/31</span>
</div>


</div>
<p class="quiet">
Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block.
</p>
<template id="filterTemplate">
<div class="quiet">
Filter:
<input oninput="onInput()" type="search" id="fileSearch">
</div>
</template>
</div>
<div class='status-line high'></div>
<pre><table class="coverage">
<tr><td class="line-count quiet"><a name='L1'></a><a href='#L1'>1</a>
<a name='L2'></a><a href='#L2'>2</a>
<a name='L3'></a><a href='#L3'>3</a>
<a name='L4'></a><a href='#L4'>4</a>
<a name='L5'></a><a href='#L5'>5</a>
<a name='L6'></a><a href='#L6'>6</a>
<a name='L7'></a><a href='#L7'>7</a>
<a name='L8'></a><a href='#L8'>8</a>
<a name='L9'></a><a href='#L9'>9</a>
<a name='L10'></a><a href='#L10'>10</a>
<a name='L11'></a><a href='#L11'>11</a>
<a name='L12'></a><a href='#L12'>12</a>
<a name='L13'></a><a href='#L13'>13</a>
<a name='L14'></a><a href='#L14'>14</a>
<a name='L15'></a><a href='#L15'>15</a>
<a name='L16'></a><a href='#L16'>16</a>
<a name='L17'></a><a href='#L17'>17</a>
<a name='L18'></a><a href='#L18'>18</a>
<a name='L19'></a><a href='#L19'>19</a>
<a name='L20'></a><a href='#L20'>20</a>
<a name='L21'></a><a href='#L21'>21</a>
<a name='L22'></a><a href='#L22'>22</a>
<a name='L23'></a><a href='#L23'>23</a>
<a name='L24'></a><a href='#L24'>24</a>
<a name='L25'></a><a href='#L25'>25</a>
<a name='L26'></a><a href='#L26'>26</a>
<a name='L27'></a><a href='#L27'>27</a>
<a name='L28'></a><a href='#L28'>28</a>
<a name='L29'></a><a href='#L29'>29</a>
<a name='L30'></a><a href='#L30'>30</a>
<a name='L31'></a><a href='#L31'>31</a>
<a name='L32'></a><a href='#L32'>32</a></td><td class="line-coverage quiet"><span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">12x</span>
<span class="cline-any cline-yes">12x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">10x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">2x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">const request = require("supertest");
const assert = require("assert");
const express = require("express");
&nbsp;
const app = express();
let requestCount = 0;
&nbsp;
// 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
app.use((req, res, next) =&gt; {
requestCount = requestCount + 1;
next();
});
&nbsp;
app.get("/user", function (req, res) {
res.status(200).json({ name: "john" });
});
&nbsp;
app.post("/user", function (req, res) {
<span class="cstat-no" title="statement not covered" > res.status(200).json({ msg: "created dummy user" });</span>
});
&nbsp;
app.get("/requestCount", function (req, res) {
res.status(200).json({ requestCount });
});
&nbsp;
// app.listen(3000);
&nbsp;
module.exports = app;
&nbsp;</pre></td></tr></table></pre>

<div class='push'></div><!-- for sticky footer -->
</div><!-- /wrapper -->
<div class='footer quiet pad2 space-top1 center small'>
Code coverage generated by
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
at 2025-01-14T17:34:00.457Z
</div>
<script src="prettify.js"></script>
<script>
window.onload = function () {
prettyPrint();
};
</script>
<script src="sorter.js"></script>
<script src="block-navigation.js"></script>
</body>
</html>

Loading