Skip to content

Commit 6d2dd0e

Browse files
committed
steps 9 and 10
1 parent 59b7330 commit 6d2dd0e

File tree

3 files changed

+73
-54
lines changed

3 files changed

+73
-54
lines changed

step10.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Step 10 - Save your blog posts
2+
3+
```bash
4+
git checkout step10
5+
git merge step9
6+
```
7+
---
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".
10+
11+
You probably want to handle the request from that `script.js` file in your server code...
12+
13+
14+
## Saving data to a file
15+
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+
18+
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.
19+
20+
```js
21+
fs.writeFile('path/to/file', yourData, (error) {
22+
23+
// do something after the file has been written
24+
});
25+
```
26+
27+
You'll note that in the `lib` folder there's a new file called `posts.json`. 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()`.
28+
29+
(If you're not sure about Javascript objects, have a chat with your mentor and your team.)
30+
31+
**Modify your server code to:**
32+
1. **Save your blogpost data into `posts.json`**
33+
2. **Handle the `script.js` request**
34+
3. **Retrieve all your posts and send them back to `script.js`**
35+
36+
If all goes well, you should have a fully functional CMS!
37+
38+
39+
🎉CONGRATULATIONS!!🎉
40+
41+
===
42+
43+
---
44+
Want more? Then head over to...
45+
46+
## [**Stretch goals >>> **](stretch.md)
47+
---
48+
## Keywords
49+
* [JSON](http://www.w3schools.com/js/js_json.asp)

step8.md

+4-54
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,17 @@ git checkout step8
55
git merge step7
66
```
77

8-
For the rest of the workshop your job is to build the proper cms. We have done frontend code for you, you only need to worry about the server side of the application.
8+
For the rest of the workshop your job is to build the proper CMS. We have done the front-end code for you - you only need to worry about the server side of the application.
99

1010
Let's clean up unnecessary files.
1111

1212
**Delete the public folder**
1313

14-
**Remove following endpoints from handler.js**:
15-
- `/node`
16-
- `/girls`
14+
**Keep `handler.js`, but delete all the code so that it's empty**
1715

16+
**Keep `server.js`, but delete all the code so that it's empty too**
1817

19-
Your handler.js should look something like this:
20-
21-
```js
22-
23-
function handler (request, response) {
24-
25-
var endpoint = request.url;
26-
console.log(endpoint);
27-
28-
if (endpoint === '/') {
29-
30-
var pathToIndex = __dirname + '/../public/index.html';
31-
32-
fs.readFile(pathToIndex, function (error, file) {
33-
response.writeHead(200, {"Content-Type": "text/html"});
34-
response.write(file);
35-
response.end();
36-
});
37-
} else if (endpoint === '/create-post') {
38-
39-
message = "";
40-
41-
request.on("data", function(data) {
42-
message += data;
43-
});
44-
45-
request.on("end", function () {
46-
message = querystring.parse(message);
47-
console.log(message.blogpost);
48-
response.end();
49-
});
50-
51-
} else {
52-
var pathToFile = __dirname + '/../public' + endpoint;
53-
var fileExtensionArray = endpoint.split('.');
54-
var fileExtension = fileExtensionArray[1];
55-
56-
fs.readFile(pathToFile, function (error, file) {
57-
58-
response.writeHead(200, { "Content-Type": "text/" + fileExtension });
59-
response.write(file);
60-
response.end();
61-
});
62-
}
63-
}
64-
65-
```
66-
67-
**Are you sure that you have deleted the public folder?**
18+
## Are you sure that you have deleted the public folder? This is really important.
6819

6920
If yes, then commit your changes:
7021

@@ -76,4 +27,3 @@ git push origin step8
7627

7728

7829
## [**Next step >>>**](step9.md)
79-

step9.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Step 9 - Build the CMS!
2+
3+
```bash
4+
git checkout step9
5+
git merge step8
6+
```
7+
8+
You'll see that there's a new public folder with different files. Run the server to see what the CMS looks like!
9+
10+
11+
*Everything is broken...*
12+
13+
Fix it!
14+
15+
Your job is to write a new server and handler. You'll serve the public assets and handle any embedded requests in the `index.html`.
16+
17+
18+
Good luck! Remember, work with your team and chat with your mentor!
19+
20+
## [**Next step >>>**](step10.md)

0 commit comments

Comments
 (0)