-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.js
36 lines (34 loc) · 1.03 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const router = (req,res)=>{
if (req.url === '/'){
res.writeHead(200,{'Content-type':'text/html'});
res.end("Hello");
} else if (req.url === '/elephants'){
res.writeHead(404);
res.end('unknown uri');
} else if (req.url === '/blog') {
if(req.url === '/blog' && req.method==='POST') {
if (req.headers.password==='potato'){
let ourData = "";
req.on("data", function(ourChunkOfData) {
ourData += ourChunkOfData;
});
req.on("end", function() {
if (ourData === JSON.stringify(['a', 'b'])) {
res.writeHead(200);
res.end(JSON.stringify(['a', 'b']));
} else {
res.writeHead(302);
res.end(JSON.stringify({Location : "/blog"}));
}
})
} else {
res.writeHead(403);
res.end('Forbidden');
}
}else {
res.writeHead(200);
res.end(JSON.stringify(["one", "two", "three"]));
}
}
}
module.exports = router;