Are you ready for challenge 11? 1
Now in this challenge I want you to put this newly created post object inside a global variable called 2
posts, with an 's' at the end. 3
We're going to have an array that is going to store all of these new posts but it's going to be a global 4
variable. 5
You need to create a new empty array that is a global variable and then you're going to add this new 6
post object to that new array. 7
And finally at the end of this route you're going to redirect to the root route. And inside the 8
root route we're going to log all of the posts inside the post array that you created. 9
Once you've completed this, what should happen is I should be able to put in a post hit publish 10
and then let's go back to compose again and let's put in another post and hit publish. 11
And firstly you notice that we get directed to the root route or the home page 12
once we've finished composing the post. And then if you take a look inside our terminal you can see that 13
my entire array is being logged 14
and the first time it only contained a single object, the 'Day 1 post, and the second time this array 15
contained two Javascript objects both of my posts, 'Day 1' and 'Day 2'. 16
This is the end goal of this challenge and it's time to pause the video and tackle this challenge. 17
All right so here's my first hint. 18
What we want to do is to be able to save this post object into an array which we can then log inside this home 19
route. 20
Now in order to do that we need a global variable. 21
Remember that global variables have to be declared outside of all of the other functions in the page. 22
So that means you probably have to do it somewhere up here 23
outside of all of the other functions and routes. 24
All right so here's hint number 2. In order to create an empty array 25
you will need to write something like this. 26
Let's say that we were creating a global var variable and we call it posts and then we would create an 27
empty array and assign it to this variable by saying = 28
[ ] ; and this would be an empty array that has nothing inside until we add something 29
to it. 30
All right so now this is my final hint and that is in order to move from the app.post to the apt.get 31
we're going to need to use something called res.redirect. And this is a method that allows you 32
to redirect to another route, 33
so essentially make a get request on any of the other routes. And if you wanted to redirect and inside 34
the parentheses you would put the route that you want to redirect to. 35
Those are all of my hints and see if now you can complete this challenge successfully.