-
Notifications
You must be signed in to change notification settings - Fork 0
/
.goutputstream-7EN711
55 lines (45 loc) · 1.42 KB
/
.goutputstream-7EN711
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const http2 = require('http2');
const fs = require('fs');
const path = require('path');
var requests=0;
const server = http2.createServer();
server.on('request', (req, res) => {
requests+=1;
console.log('Incoming request number ',requests,' headers:', req.headers);
switch (req.method) {
case 'GET':
const imageStream = fs.createReadStream('image.jpg');
res.writeHead(200, { 'Content-Type': 'image/jpg' });
imageStream.pipe(res);
// res.setHeader('Content-Type', 'image/jpg');
// fs.createReadStream('image.jpg').pipe(res);
break;
case 'DELETE':
console.log('I emulate this deletion');
res.end();
break;
case 'PUT':
console.log(`I emulate this ${req.method} method`);
res.end();
case 'POST':
var result=0;
console.log(`I emulate this ${req.method} method`);
for(var i=0;i<50;i++){
const randomNumber = Math.floor(Math.random() * 10);
result += Math.pow(randomNumber, 8);
result/=2;
}
res.end();
break;
default:
res.statusCode = 400;
res.end();
}
});
server.on('error',(error)=>{
console.error(`An error occurred: ${error}`);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('An error occurred on the server. Please try again later.');
})
console.log("Server listening on port 8000");
server.listen(8000);