-
Notifications
You must be signed in to change notification settings - Fork 0
/
myserver.js
72 lines (52 loc) · 2.16 KB
/
myserver.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var https = require('http2');
var fs = require('fs');
var winston = require("winston");
var mime = require ("mime");
var url = require("url");
var path = require("path");
var options = {
// use your key and cert here. the key and cert file names are place holder only.
key: fs.readFileSync('my.key.pem'),
cert: fs.readFileSync('my.cert.pem')
};
function handleGet(req, res) {
var parsedRequestURI = url.parse(req.url);
var filename = path.join(process.cwd(), parsedRequestURI.pathname);
var pushedfilename = path.join(process.cwd(), "images/mustang_frm_pushed.jpg");
console.log("filename =" + filename );
if(res.push&&parsedRequestURI.pathname == "/index.html"){
var aPushedReadStream =fs.createReadStream(pushedfilename);
aPushedReadStream.on('open', function (){
var push = res.push("/images/mustang_frm_pushed.jpg");
push.writeHead(200, {"Content-Type": mime.lookup(pushedfilename)});
aPushedReadStream.pipe(push);
//console.log ("pushed images");
});
aPushedReadStream.on('end', function (){
//push.end();
console.log(" finished pushing images to client." );
});
}
var aReadStream =fs.createReadStream(filename);
aReadStream.on('open', function (){
res.writeHead(200, {"Content-Type": mime.lookup(filename)});
aReadStream.pipe(res);
});
aReadStream.on('end', function (){
console.log(" finished streaming"+ parsedRequestURI.pathname+" to client." );
res.end();
});
aReadStream.on('error', function (err){
console.log (err);
res.writeHead(404, {"Content-Type": mime.lookup(filename)});
res.end(" console.log (error occurred )");
});
}
function handleClientRequest (req , res){
if (req.method == "GET") {
console.log("Received GET request for URL: " + req.url);
handleGet(req , res);
}
}
var aHttpsServer = https.createServer(options, handleClientRequest);
aHttpsServer.listen(4433);