Skip to content

Commit df1da9c

Browse files
committed
adds more info for step 10, small README updates
1 parent f111cdc commit df1da9c

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Make sure to checkout to the relevant branch before continuing.
1717

1818
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.
1919

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+
2022

2123
Your mentor is there to help you. Don't let them die of boredom - talk to them and ask questions!
2224

step10.md

+31-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ git merge step9
66
```
77
---
88

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".
1010

1111
You probably want to handle the request from that `script.js` file in your server code...
1212

@@ -15,6 +15,24 @@ You probably want to handle the request from that `script.js` file in your serve
1515

1616
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.
1717

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+
1836
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.
1937

2038
```js
@@ -24,27 +42,31 @@ fs.writeFile('path/to/file', yourData, (error) {
2442
});
2543
```
2644

27-
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.)
3245

3346
**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**
3649
3. **Retrieve all your posts and send them back to `script.js`**
3750

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+
3859
If all goes well, you should have a fully functional CMS!
3960

4061

4162
🎉CONGRATULATIONS!!🎉
4263
===
4364

44-
---
65+
4566
Want more? Then head over to...
4667

4768
## [**Stretch goals >>> **](stretch.md)
4869
---
4970
## Keywords
5071
* [JSON](http://www.w3schools.com/js/js_json.asp)
72+
* [timestamp](http://www.w3schools.com/jsref/jsref_now.asp)

stretch.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ It would be a great idea to create a new branch on Git for yourself, so you can
1313
### More modularisation!
1414
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.
1515

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`
1717

1818

1919
### 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.
2121

2222
* [MongoDB](https://docs.mongodb.org/getting-started/node/)
2323
[Redis]

0 commit comments

Comments
 (0)