-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarter.js
More file actions
50 lines (42 loc) · 1.46 KB
/
starter.js
File metadata and controls
50 lines (42 loc) · 1.46 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
var http = require('http');
var url = require('url');
var index = require('./index');
var fs = require('fs');
var renderhtml = require('./renderhtml');
var server = http.createServer();
function control(request, response) {
var vars = url.parse(request.url);
if(request.method === 'POST'){
var requestBody = "";
request.on('data', function(data){
requestBody += data;
});
request.on('end', function(data){
response.writeHead(200, {'content-type': 'text/html'});
})
}
if (vars.pathname.indexOf(".html") != -1 || vars.pathname == "/") {
console.log(vars.pathname);
response.writeHead(200, {
'content-type': 'text/html'
});
var path = vars.pathname.split("/");
var file = path[path.length - 1];
if(file == "index.html" || vars.pathname == "/")
index.render(request, response);
else
renderhtml.render(request, response, file);
} else if (vars.pathname.indexOf(".") != -1) { /* Images rendering */
var path = vars.pathname.split(".");
var extension = path[path.length - 1];
response.writeHead(200, {
'content-type': 'image/' + extension
});
var img = fs.readFileSync("." + vars.pathname);
response.end(img, 'binary')
} else {
renderhtml.notFound(response);
}
}
server.on('request', control);
server.listen(8080);