Skip to content

Commit b6537c2

Browse files
save file
1 parent 5f533d8 commit b6537c2

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

blog/25-07-24/http-server/ex/http-server-minimal.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,27 @@
66
console.log(`http://localhost:${port}/`);
77
function request(req,res){
88
console.log(req.method,req.url);
9-
var url = req.url.slice(1);
10-
11-
if(url==''){
12-
res.writeHead(200,{'content-type':'text/html'}).end(html);
13-
return;
14-
}
15-
16-
var ext = url.split('.').at(-1),type;
17-
switch(ext){
18-
19-
case 'html' : type='text/html';
20-
case 'js' : type='text/javascript'; break;
21-
case 'css' : type='text/css'; break;
22-
23-
}//switch
9+
var path = req.url.slice(1);
10+
var type = mime(path);
2411

2512
res.writeHead(200,{'content-type':type});
2613
require('fs').createReadStream(url).pipe(res);
2714

2815
}//request
2916

17+
function mime(path){
3018

31-
var html = `
32-
<style>html{font-family:arial}body{margin:20px}</style>
33-
<h3>It Works!</h3>
34-
`;
35-
19+
var ext = path.split('.').at(-1),type;
20+
switch(ext){
21+
22+
case 'html' : return 'text/html';
23+
case 'js' : return 'text/javascript';
24+
case 'css' : return 'text/css';
25+
26+
}//switch
27+
return 'application/octet-stream';
28+
29+
}//mime
3630

3731

3832

0 commit comments

Comments
 (0)