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
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!
Copy file name to clipboardexpand all lines: step8.md
+4-54
Original file line number
Diff line number
Diff line change
@@ -5,66 +5,17 @@ git checkout step8
5
5
git merge step7
6
6
```
7
7
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.
9
9
10
10
Let's clean up unnecessary files.
11
11
12
12
**Delete the public folder**
13
13
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**
17
15
16
+
**Keep `server.js`, but delete all the code so that it's empty too**
18
17
19
-
Your handler.js should look something like this:
20
-
21
-
```js
22
-
23
-
functionhandler (request, response) {
24
-
25
-
var endpoint =request.url;
26
-
console.log(endpoint);
27
-
28
-
if (endpoint ==='/') {
29
-
30
-
var pathToIndex =__dirname+'/../public/index.html';
0 commit comments