-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (53 loc) · 1.9 KB
/
app.js
File metadata and controls
65 lines (53 loc) · 1.9 KB
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
var http = require('http');
var url = require('url');
var template_fs = require("fs");
var path = require("path");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var url_path = url.parse(req.url);
var media_resource_expression = /.css|.png|.gif|.js/gi;
var ext = path.extname(url_path.pathname);
var getContent_type = function(ext){
var Content_type_list = {'.html':'text/html','.htm':'text/html','.png':'image/png','.css':'text/css','.js':'application/x-javascript','.gif':'image/gif'};
Content_type_list = eval(Content_type_list);
return Content_type_list[ext];
};
if (ext.length > 0) {
res.writeHead(200,{'Content-Type':" "+getContent_type(path.extname(url_path.pathname))});
}
else {
res.writeHead(200,{'Content-Type':' text/html'});
}
if ( media_resource_expression.test(path.extname(url_path.pathname)) ) {
var filename = url_path.pathname.split('');
filename[0] = '';
template_fs.readFile(filename.join(''),"", function (err, data) {
res.end(data);
});
}
else {
switch (url_path.pathname ) {
case "/admin":
res.end('admin page'+url_path.pathname+"\nquery_string:"+url_path.query);
break;
case "/index":
template_fs.readFile('index.html',"utf8", function (err, data) {
if (err) throw err;
res.end(data);
});
break;
case "/user":
res.data("NULL");
break;
default:
res.end('un'+url_path.pathname);
break;
}
}
}).listen(process.env.VMC_APP_PORT || 1337, null);
function readFile (file) {
template_fs.readFile(file,"utf8", function (err, data) {
if (err) throw err;
return data;
});
}