You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+2
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,8 @@ Make sure to checkout to the relevant branch before continuing.
17
17
18
18
You will see code snippets throughout the walkthrough. Try to resist the urge to copy and paste - you will learn **much** more if you get into the habit of typing things out.
19
19
20
+
You might want to test out small bits of code and run them before adding it to your project. For this, you can use [repl.it](https://repl.it/) if you like.
21
+
20
22
21
23
Your mentor is there to help you. Don't let them die of boredom - talk to them and ask questions!
Copy file name to clipboardexpand all lines: step10.md
+31-9
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ git merge step9
6
6
```
7
7
---
8
8
9
-
There's a new file in the public folder, `script.js`. Don't worry about what all the code means, just know that it's responsible for sending a request to `GET` old blogposts and display them on the page underneath "Recent Posts".
9
+
There's a new file in the public folder, `script.js`. Don't worry about what all the code means, just know that it's responsible for sending a request to `GET` old blog posts and display them on the page underneath "Recent Posts".
10
10
11
11
You probably want to handle the request from that `script.js` file in your server code...
12
12
@@ -15,6 +15,24 @@ You probably want to handle the request from that `script.js` file in your serve
15
15
16
16
At the moment, your blog posts are reaching the server, but aren't being saved anywhere. They just disappear into a cloud of bits and bytes. We need to find a way to save them so that you can retrieve them later.
17
17
18
+
You'll note that in the `lib` folder there's a new file called `posts.json`.
19
+
20
+
JSON is a type of file for structuring data in a readable way. They're also a really popular format for sending data across the web.
21
+
22
+
JSON is a string representation of a Javascript object. JSON objects convert really easily to Javascript objects, and vice versa, with `JSON.parse()` and `JSON.stringify()`.
23
+
24
+
(If you're not sure about Javascript objects, have a chat with your mentor and your team.)
25
+
26
+
You'll see there's already one blog post sitting in `posts.json`. The format is:
27
+
```js
28
+
{
29
+
[timestamp]: [blog post message]
30
+
}
31
+
```
32
+
We've used a timestamp as the key so that the blog posts are listed in chronological order. Also, it's a record of when the blog post was created.
33
+
34
+
To add your own blog posts to `posts.json`, you will need to read the file from the hard drive, add to it, then write it back.
35
+
18
36
You'll remember that `fs.readFile()` is the method responsible for reading files from your hard drive. Well, `fs.writeFile()` is a method that allows you to write data into a file.
You'll note that in the `lib` folder there's a new file called `posts.json`.
28
-
29
-
JSON is a type of file for structuring data in a readable way. JSON objects convert really easily to Javascript objects, and vice versa, with `JSON.parse()` and `JSON.stringify()`.
30
-
31
-
(If you're not sure about Javascript objects, have a chat with your mentor and your team.)
32
45
33
46
**Modify your server code to:**
34
-
1.**Save your blogpost data into `posts.json`**
35
-
2.**Handle the `script.js` request**
47
+
1.**Save your blog post data into `posts.json`**
48
+
2.**Handle the `script.js` request coming into the server**
36
49
3.**Retrieve all your posts and send them back to `script.js`**
37
50
51
+
52
+
* You'll want to make sure your blog posts are also added with a timestamp. You can get the current unix timestamp using `Date.now()`.
53
+
54
+
*`fs.writeFile()` automatically overwrites the whole file. Chances are you don't want to lose all your old blog posts, so think about how you can use both `fs.readFile()` and `fs.writeFile()` to prevent overwriting.
55
+
56
+
* The code in `script.js` is expecting to receive a JSON object. Remember your http headers!
57
+
---
58
+
38
59
If all goes well, you should have a fully functional CMS!
Copy file name to clipboardexpand all lines: stretch.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -13,11 +13,11 @@ It would be a great idea to create a new branch on Git for yourself, so you can
13
13
### More modularisation!
14
14
Currently, `handler.js` is a combination of decisions and actions. The decisions are the the if-else branches that look at the request url, and the actions are the bits inside the curly brackets.
15
15
16
-
You could split out the decision part to its own file, `routes.js`. Then the actual actions (so, the functions you call) would remain in `handler.js`. `routes.js`
16
+
You could split out the decision part to its own file, `routes.js`. Then the actual actions (so, the functions you call) would remain in `handler.js`. `routes.js`
17
17
18
18
19
19
### Use a database
20
-
Instead of writing to a file on your hard drive, you could save your blogposts in a database, which would be much quicker in terms of performance if there was a lot of data.
20
+
Instead of writing to a file on your hard drive, you could save your blog posts in a database, which would be much quicker in terms of performance if there was a lot of data.
0 commit comments