-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
102 lines (100 loc) · 3.04 KB
/
backend.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const http = require('http');
const fs = require('fs')
const url = require('url');
var querystring = require('querystring');
const server = http.createServer(function(req, res) {
const page = url.parse(req.url).pathname;
var params = querystring.parse(url.parse(req.url).query);
console.log(page);
if (page == '/') {
fs.readFile('indexA.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}
else if (page == '/indexB') {
fs.readFile('indexB.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}
else if (page == '/indexC') {
fs.readFile('indexC.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}else if (page == '/api') {
if('profile' in params){
if(params['profile']== 'dreads'){
res.writeHead(200, {'Content-Type': 'application/json'});
var objToJson = {
hairType: "dreads",
name: "derek",
status: "Rasta",
favoriteFood: "Popcorn"
}
res.end(JSON.stringify(objToJson));
}//student = leon
else if(params['profile'] != 'dreads'){
res.writeHead(200, {'Content-Type': 'application/json'});
var objToJson = {
hairType: "unknown",
name: "unknown",
status: "unkown",
favoriteFood: "unknown"
}
res.end(JSON.stringify(objToJson));
}//student != leon
}//student if
}//else if
else if (page == '/css/styleA.css'){
fs.readFile('css/styleA.css', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/css'});
res.write(data);
res.end();
});
}else if (page == '/css/styleB.css'){
fs.readFile('css/styleB.css', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/css'});
res.write(data);
res.end();
});
}else if (page == '/css/styleC.css'){
fs.readFile('css/styleC.css', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/css'});
res.write(data);
res.end();
});
}else if (page == '/js/mainA.js'){
fs.readFile('js/mainA.js', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.write(data);
res.end();
});
}else if (page == '/js/mainB.js'){
fs.readFile('js/mainB.js', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.write(data);
res.end();
});
}else if (page == '/js/mainC.js'){
fs.readFile('js/mainC.js', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.write(data);
res.end();
});
}else if (page == '/js/mainD.js'){
fs.readFile('js/mainD.js', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.write(data);
res.end();
});
}else{
res.write("404");
res.end();
};
});
server.listen(8000);