-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.js
76 lines (65 loc) · 2.18 KB
/
apps.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
73
74
75
76
var http = require('http');
var fs = require('fs');
function getStrArr(str){
let strArr = str.split('/');
let temp = strArr[strArr.length-1].split('.');
strArr.pop();
strArr = strArr.concat(temp);
return strArr;
}
var server = http.createServer(function (request, response) {
console.log('client request URL:', request.url);
var urlArr = getStrArr(request.url);
if(request.url =='/'){
fs.readFile('views/index.html', 'utf8', function(errors, contents){
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(contents);
response.end();
});
}
else if(request.url === '/cars'){
fs.readFile('views/cars.html', 'utf8', function(errors, contents){
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(contents);
response.end();
});
}
else if(request.url === '/cats'){
fs.readFile('views/cats.html', 'utf8', function(errors, contents){
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(contents);
response.end();
});
}
else if(request.url === '/cars/new'){
fs.readFile('views/addCar.html', 'utf8', function(errors, contents){
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(contents);
response.end();
});
}
else if(urlArr[urlArr.length-1] === 'css'){
let filePath = '.';
filePath += request.url;
fs.readFile(filePath, 'utf8', function(errors, contents){
response.writeHead(200, {'Content-type': 'text/css'});
response.write(contents);
response.end();
})
}
else if(urlArr[urlArr.length-1] === 'jpg'){
let filePath = '.';
filePath += request.url;
console.log(filePath);
fs.readFile(filePath, function(errors, contents){
response.writeHead(200, {'Content-type': 'image/jpg'});
response.write(contents);
response.end();
})
}
else{
response.end('File Not Found');
}
});
server.listen(7077);
console.log('Running in localhost at port 7077');