-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsteps.txt
127 lines (110 loc) · 4.13 KB
/
steps.txt
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
1. create a router
2. rename handler function to router
3. move router function to router.js and export it
4. import router in server.js
(uninstall nodemon and do npx nodemon server.js instead)
5. in router - import fs and path
6. make sure querystring variable is in create post route
Break down module
homeHandler
1. Create a handlers folder
2. touch homeHandler.js and create a homeHandler function
3. move all logic from endpoint / to homeHandler
4. in router.js import homeHandler insert it in the endpoint / and passon filePath as a variable
creat-post
1. in handlers folder touch createPost
2. in createPost create a function createPost && paste the logic thenexport it
3 import the function in router
4. in router add (method === "POST" && endpoint === "/create-post") to the conditional
add missing handler - follow same steps as above
5. on('end') add the below to write a file with blog entries
const data = new URLSearchParams(body);
const name = data.get("blogpost");
fs.appendFile("log.txt", name, function(err) {
if (err) {
// append failed
} else {
response.writeHead(200, { "content-type": "text/html" });
response.end(`<h1>Hello ${name}</h1>`);
}
});
fs.readFile("./public/index.html", "utf8", (err, html) => {
if (err) {
throw err;
}
const root = parse(html);
const body = root.querySelector("body");
// body.set_content('<div id = "asdf"></div>');
body.appendChild('<div id = "sdfdffd"></div>');
console.log(root.toString()); // This you can write back to file!
fs.writeFile("./public/index.html", result, "utf8", function(err) {
if (err) return console.log(err);
response.writeHead(302, { location: "/" });
response.end();
});
});
const querystring = require("querystring");
const fs = require("fs");
function createPost(request, response) {
let body = "";
request.on("data", function(chunkOfData) {
body += chunkOfData;
});
request.on("end", function() {
// let convertedData = querystring.parse(allTheData);
// console.log(convertedData);
response.writeHead(302, { location: "/" });
response.end();
const data = new URLSearchParams(body);
const name = data.get("blogpost");
// fs.appendFile("./data/blogPosts.txt", name, function(err) {
// if (err) {
// // append failed
// } else {
// // response.writeHead(200, { "content-type": "text/html" });
// // response.end(`<h1>Hello ${name}</h1>`);
// response.writeHead(302, { location: "/" });
// response.end();
// }
// });
// fs.readFile("./public/index.html", "utf8", function(err, data) {
// if (err) {
// return console.log(err);
// }
// var toPrepand = "<script>console.log('hello world')</script>";
// data = data.replace(/\<\/body>/g, toPrepand + "</body>");
// console.log(data);
// response.writeHead(302, { location: "/" });
// response.end();
// });
// fs.readFile("./public/index.html", "utf8", function(err, data) {
// if (err) {
// return console.log(err);
// }
// var toPrepand = "blah";
// var result = data.replace(/\<\/body>/g, toPrepand + "</body>");
// fs.writeFile("./public/index.html", result, "utf8", function(err) {
// if (err) return console.log(err);
// response.writeHead(302, { location: "/" });
// response.end();
// });
// });
const fs = require("fs");
const parse = require("node-html-parser").parse;
fs.readFile("./public/index.html", "utf8", (err, html) => {
if (err) {
throw err;
}
const root = parse(html);
const body = root.querySelector("body");
// body.set_content('<div id = "asdf"></div>');
body.appendChild('<div id = "sdfdffd"></div>');
console.log(root.toString()); // This you can write back to file!
fs.writeFile("./public/index.html", result, "utf8", function(err) {
if (err) return console.log(err);
response.writeHead(302, { location: "/" });
response.end();
});
});
}
module.exports = createPost;