forked from node-girls/node-workshop
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
82 lines (68 loc) · 2.17 KB
/
server.js
File metadata and controls
82 lines (68 loc) · 2.17 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var http = require('http');
var fs = require('fs');
var querystring = require('querystring');
// var message = 'I am so happy to be part of the Node Girls workshop!';
function handler (request, response) {
var endpoint = request.url;
console.log(endpoint);
var method = request.method;
console.log(method);
if (endpoint === "/") {
response.writeHead(200, {"Content-Type": "text/html"});
fs.readFile(__dirname + '/public/index.html', function(error, file) {
if (error) {
console.log(error);
return;
}
response.end(file);
});
}
else if (endpoint === "/node") {
// console.log("wow node time");
response.writeHead(200, {"Content-Type": "text/html"})
response.write("wow node time");
response.end();
} else if (endpoint === "/girls") {
// console.log("node girls just wanna have fun");
response.writeHead(200, {"Content-Type": "text/html"})
response.write("node girls just wanna have fun");
response.end();
} else if (endpoint === "/create-post" && method === "POST") {
var allTheData = '';
request.on('data', function (chunkOfData) {
allTheData += chunkOfData;
});
request.on('end', function () {
console.log(allTheData);
var convertedData = querystring.parse(allTheData);
console.log(convertedData);
response.writeHead(301, {"Location": "/"})
response.end();
});
}
else {
// console.log(endpoint)
fs.readFile(__dirname + '/public/' + endpoint, function(error, file) {
if (error) {
response.writeHead(404, {"Content-Type": "text/html"});
response.end();
}
else {
const extension = endpoint.split(".")[1];
const extensionType = {
css: "text/css",
js: "application/javascript",
ico: "image/x-icon",
jpg: "image/jpeg",
png: "image/png"
};
response.writeHead(200, {"Content-Type": extensionType[extension]} )
response.end(file);
}
})
}
}
var server = http.createServer(handler);
server.listen(3000, function () {
console.log("Server is listening on port 3000. Ready to accept requests!");
});