Workshop Steps
- 0 - Environment Setup
- 1 - Setting up your project
- 2 - Installing Express
- 3 - Building the server
- 4 - Talking to the server
- 5 - Serving static files
- 6 - Displaying your posts
- 7 - Saving New Posts (1/4)
- 8 - Saving New Posts (2/4)
- 9 - Saving New Posts (3/4)
- 10 - Saving New Posts (4/4)
- 11 - Congratulations
Challenges
Useful Links
Step 8 - Saving New Posts (2/4)
Ok we’ve got our form data. The next thing we need to do is construct the object that will hold that data. This is the object that we will be adding to our saved data and that we will be sending back as the response.
The client expects the /create-post
endpoint to send back the object that we will be adding to our posts.json
file.
Making a new post object
Our blog post objects have two fields: timestamp
and content
.
timestamp is a number that tells us the exact time that the post was made. It’s actually a number instead of anything you will recognise as a date - the number of milliseconds since 1 January 1970 (UTC). We can get this value using the function Date.now()
.
content is the post that was sent. We get that from request.fields.blogpost
.
We use both of those values to put together a new object which we call newPost
.
Update your /create-post
route like below:
app.post('/create-post', function (request, response) {
var now = Date.now();
var newPost = {
timestamp: now,
content: request.fields.blogpost
}
});
Sending the new post object back
Now that we have our new post object we have to send it back in the response.
We do that with response.send()
.
Update your /create-post
route like below:
app.post('/create-post', function (request, response) {
var now = Date.now();
var newPost = {
timestamp: now,
content: request.fields.blogpost
}
response.send(newPost);
});
The client expects the response to contain the object converted into a JSON string. response.send()
automatically does that conversion for you if you pass it an object.
Test it out
Restart your server again and try to create some new posts.
You should see your new posts added to the page.
But remember that we haven’t actually saved the new post yet - if you refresh the page, they all disappear.
If it isn’t working for you, you can double-check that your code is right with the solution below:
var express = require('express');
var formidable = require('express-formidable');
var fs = require('fs');
var app = express();
app.use(express.static('public'));
app.use(formidable());
app.post('/create-post', function (request, response) {
var now = Date.now();
var newPost = {
timestamp: now,
content: request.fields.blogpost
}
response.send(newPost);
});
app.get('/get-posts', function (request, response) {
fs.readFile(__dirname+'/data/posts.json', function (error, data) {
if (error) {
console.log('Error reading posts.json: '+error);
response.status(500);
response.send(error);
} else {
response.send(data);
}
});
});
app.listen(8080, function () {
console.log('Server has started listening on port 8080.');
});
Keyword | Explanation |
---|---|
Date | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date |