-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (117 loc) · 3.96 KB
/
Copy pathindex.js
File metadata and controls
127 lines (117 loc) · 3.96 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// import { createSecureServer, constants } from "http2";
import { createServer } from "https";
import { readFile, copyFileSync, readFileSync } from "fs";
import formidable from "formidable";
import data from "./data.js";
import { getContacts } from "./list.js";
import { deleteContact } from "./delete.js";
import { getContactForm } from "./form.js";
import { saveContact } from "./save.js";
const options = {
key: readFileSync("./localhost.key"),
cert: readFileSync("./localhost.cert"),
};
// const { HTTP2_HEADER_PATH, HTTP2_HEADER_STATUS, HTTP2_HEADER_METHOD } =
// constants;
// const server = createSecureServer(options);
// server
// .on("stream", (stream, headers) => {
// const parts = headers[HTTP2_HEADER_PATH].split("/");
// if (headers[HTTP2_HEADER_PATH] === "/favicon.ico") {
// readFile("public/favicon.ico", (err, data) => {
// if (err) {
// stream.respond({
// "content-type": "text/plain",
// [HTTP2_HEADER_STATUS]: 404,
// });
// stream.end();
// } else {
// stream.end(data);
// }
// });
// } else if (parts.includes("assets")) {
// readFile(
// `public${headers[HTTP2_HEADER_PATH].replaceAll("%20", " ")}`,
// (err, data) => {
// if (err) {
// stream.respond({ [HTTP2_HEADER_STATUS]: 404 });
// stream.end();
// } else {
// stream.end(data);
// }
// }
// );
// } else {
// sendHttp2Response(stream, getContacts(data.contacts));
// }
// })
// .listen(8080, () => {
// console.log(`Contacts App running at https://localhost:8080`);
// });
createServer(options, (request, response) => {
const urlParts = request.url.split("/");
if (urlParts.includes("delete")) {
data.contacts = deleteContact(data.contacts, urlParts[2]);
redirect(response, "/");
} else if (urlParts.includes("new")) {
sendResponse(response, getContactForm(data.contacts));
} else if (urlParts.includes("edit")) {
sendResponse(response, getContactForm(data.contacts, urlParts[2]));
} else if (urlParts.includes("save") && request.method === "POST") {
const form = formidable({});
form.parse(request, (err, contact, files) => {
if (files.profilePic) {
copyFileSync(
files.profilePic[0].filepath,
`public/assets/${files.profilePic[0].originalFilename}`
);
contact["file"] = `/assets/${files.profilePic[0].originalFilename}`;
}
const newContact = {
id: contact.id[0],
firstName: contact.firstName[0],
lastName: contact.lastName[0],
phone: contact.phone[0],
email: contact.email[0],
file: contact.file,
};
saveContact(data.contacts, newContact);
redirect(response, "/");
});
} else if (urlParts.includes("assets")) {
readFile(`public${request.url.replaceAll("%20", " ")}`, (err, data) => {
if (err) {
response.statusCode = 404;
response.end();
} else {
response.end(data);
}
});
} else if (request.url === "/favicon.ico") {
readFile("public/favicon.ico", "utf-8", (err, data) => {
if (err) {
response.statusCode = 404;
response.end();
} else {
response.end(data);
}
});
} else {
response.writeHead(200, { "Content-Type": "text/html" });
sendResponse(response, getContacts(data.contacts));
}
}).listen(8080, () => {
console.log(`Contacts App is running at https://localhost:8080`);
});
function sendResponse(response, responseBody) {
response.writeHead(200, { "Content-Type": "text/html" });
response.end(responseBody);
}
function redirect(response, to) {
response.writeHead(302, { location: to, "content-type": "text/plain" });
response.end(`redirecting to ${to}`);
}
function sendHttp2Response(stream, responseBody) {
stream.respond({ "content-type": "text/html", [HTTP2_HEADER_STATUS]: 200 });
stream.end(responseBody);
}