-
Notifications
You must be signed in to change notification settings - Fork 0
/
app1.js
55 lines (43 loc) · 1.44 KB
/
app1.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
var http=require("http")
var url= require("url")
var fs=require("fs")
var index_route = require('./index.js')
var file_route = require('./file.js')
var url_route = require('./url.js')
http.createServer(function (req, res) {
res.setHeader('Access-Control-Allow-Origin','https://github.com')
path_name = url.parse(req.url).pathname
switch(path_name){
case '/':
index_route.render_html('index.html',res)
break
case '/file':
file_route.download(req,res)
break
case '/url':
url_route.parse_get(req,res)
break
}
if(path_name.endsWith('.js')){
fs.readFile(path_name.substring(1), function(err, data) {
stat = fs.statSync(path_name.substring(1))
res.setHeader('Last-Modified',stat.mtime)
res.setHeader('Content-Type', 'text/javascript')
if(err){
return console.error(err)
}
res.write(data);
res.end()
});
}
else if(path_name.endsWith('.png')){
fs.readFile(path_name.substring(1), function(err, data) {
res.setHeader('Content-Type', 'image/png')
if(err){
return console.error(err)
}
res.write(data);
res.end()
});
}
}).listen(process.env.PORT || 8000);