All right, let's go through the answer to this challenge. 1
The steps that we need to perform are we need to create a global container inside our app.js to 2
be able to store each of the posts that come in through this post route 3
when we compose a new blog post. And then we need to be able to redirect from this post route to our get 4
route for the home page or the root route. 5
And there we're going to console log all of the posts that we've saved. 6
So the first step is creating that global variable. You might have created something using a var 7
and we said that we should call it posts which is plural to save all of our individual posts. And then 8
we're going to set it to an empty array to begin with. 9
So now inside our post route we're able to tap into that posts array, 10
remember the s. 11
And then we're going to use the push method to add elements into the array And you can easily find this 12
out 13
if you google for how to add new items to an array for example. There's a lot of methods and a lot of keywords 14
but you don't have to remember any of them. 15
Remember that programming is just like an open book exam. 16
Everything is out there on the Internet for you to find and you don't have to resort to memorizing these 17
things at all. 18
You just have to know that they exist and roughly how they work. 19
So now let's use this method push to add our you post item into it. 20
At this stage we should at least have one item inside our new post array. 21
Next it's time to use res.redirect in order to send our user back to the home page or the root 22
route. 23
And what this line will do is it will take us right over here and jump into this root route. 24
It's at this stage where we'll console log our posts array so that we see all the posts that are currently 25
stored inside. 26
So now let's give it a spin. 27
If we head over to our compose page and we add some mumbo jumbo in here, hit publish, we get redirect back 28
to the home route and inside our console we get that new post printed and you can see that denoted by 29
the square brackets, 30
this is inside an array. 31
The final thing that I would like to change is we know that this has to be a global variable, 32
it has to be reached by all of these functions. 33
So this is why it's right up at the top and it's outside of any of these other functions. 34
The only thing I would change about this as we mentioned many times before, 35
instead of using a var, you want to go from a let. 36
So a let is a safer version of a var. 37
So this is the solution to challenge 11.