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

week-3 middlewares #1165

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion week-3/01-middlewares/01-requestcount.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ 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(function(req,res,next){
requestCount=requestCount+1;
next();
})
app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
});
Expand Down
10 changes: 9 additions & 1 deletion week-3/01-middlewares/02-ratelimitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ let numberOfRequestsForUser = {};
setInterval(() => {
numberOfRequestsForUser = {};
}, 1000)

app.use((req,res,next)=>{
const id = req.header["user-id"];
numberOfRequestsForUser[id]=numberOfRequestsForUser[id] ||0;
if(numberOfRequestsForUser[id]>=5){
return res.status(404).json({msg:"Rate limit Exceeded"});
}
numberOfRequestsForUser[id]++;
next();
})
app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
});
Expand Down
4 changes: 4 additions & 0 deletions week-3/01-middlewares/03-errorcount.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ app.get('/user', function(req, res) {
app.post('/user', function(req, res) {
res.status(200).json({ msg: 'created dummy user' });
});
app.use((err,req,res,next)=>{
res.status(404).send("Error")
errorCount+=1

})
app.get('/errorCount', function(req, res) {
res.status(200).json({ errorCount });
});
Expand Down
39 changes: 39 additions & 0 deletions week-3/01-middlewares/coverage/clover.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1737665762744" clover="3.2.0">
<project timestamp="1737665762744" name="All files">
<metrics statements="30" coveredstatements="28" conditionals="4" coveredconditionals="3" methods="0" coveredmethods="0" elements="34" coveredelements="31" complexity="0" loc="30" ncloc="30" packages="1" files="1" classes="1"/>
<file name="03-errorcount.js" path="C:\web dev css\assignments\week-3\01-middlewares\03-errorcount.js">
<metrics statements="30" coveredstatements="28" 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="cond" truecount="1" falsecount="0"/>
<line num="22" count="11" type="stmt"/>
<line num="23" count="11" type="stmt"/>
<line num="24" count="11" type="stmt"/>
<line num="25" count="11" type="stmt"/>
<line num="26" count="1" type="cond" truecount="1" falsecount="0"/>
<line num="27" count="2" type="stmt"/>
<line num="28" count="1" type="stmt"/>
<line num="29" count="1" type="stmt"/>
<line num="30" 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:\\web dev css\\assignments\\week-3\\01-middlewares\\03-errorcount.js": {"path":"C:\\web dev css\\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":37}},"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":38}},"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":29}},"21":{"start":{"line":22,"column":0},"end":{"line":22,"column":31}},"22":{"start":{"line":23,"column":0},"end":{"line":23,"column":15}},"23":{"start":{"line":24,"column":0},"end":{"line":24,"column":0}},"24":{"start":{"line":25,"column":0},"end":{"line":25,"column":2}},"25":{"start":{"line":26,"column":0},"end":{"line":26,"column":43}},"26":{"start":{"line":27,"column":0},"end":{"line":27,"column":39}},"27":{"start":{"line":28,"column":0},"end":{"line":28,"column":3}},"28":{"start":{"line":29,"column":0},"end":{"line":29,"column":0}},"29":{"start":{"line":30,"column":0},"end":{"line":30,"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":11,"22":11,"23":11,"24":11,"25":1,"26":2,"27":1,"28":1,"29":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":21,"loc":{"start":{"line":21,"column":8},"end":{"line":25,"column":2}},"locations":[{"start":{"line":21,"column":8},"end":{"line":25,"column":2}}]},"3":{"type":"branch","line":26,"loc":{"start":{"line":26,"column":23},"end":{"line":28,"column":2}},"locations":[{"start":{"line":26,"column":23},"end":{"line":28,"column":2}}]}},"b":{"0":[11],"1":[0],"2":[11],"3":[2]},"fnMap":{},"f":{}}
}
166 changes: 166 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,166 @@

<!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.42% </span>
<span class="quiet">Statements</span>
<span class='fraction'>27/28</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.42% </span>
<span class="quiet">Lines</span>
<span class='fraction'>27/28</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></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">12x</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></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(function(req,res,next){
requestCount=requestCount+1;
next();
})
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;
module.exports = app;</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-23T20:40:19.974Z
</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