0 1 00:00:00,390 --> 00:00:03,200 All right, let's go through the answer to this challenge. 1 2 00:00:03,360 --> 00:00:10,290 The steps that we need to perform are we need to create a global container inside our app.js to 2 3 00:00:10,290 --> 00:00:15,260 be able to store each of the posts that come in through this post route 3 4 00:00:15,450 --> 00:00:23,700 when we compose a new blog post. And then we need to be able to redirect from this post route to our get 4 5 00:00:23,700 --> 00:00:27,470 route for the home page or the root route. 5 6 00:00:27,780 --> 00:00:32,130 And there we're going to console log all of the posts that we've saved. 6 7 00:00:32,460 --> 00:00:39,010 So the first step is creating that global variable. You might have created something using a var 7 8 00:00:39,330 --> 00:00:46,740 and we said that we should call it posts which is plural to save all of our individual posts. And then 8 9 00:00:46,740 --> 00:00:49,850 we're going to set it to an empty array to begin with. 9 10 00:00:50,310 --> 00:00:57,190 So now inside our post route we're able to tap into that posts array, 10 11 00:00:57,390 --> 00:00:58,560 remember the s. 11 12 00:00:58,590 --> 00:01:06,540 And then we're going to use the push method to add elements into the array And you can easily find this 12 13 00:01:06,540 --> 00:01:06,790 out 13 14 00:01:06,840 --> 00:01:13,940 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 15 00:01:14,220 --> 00:01:16,210 but you don't have to remember any of them. 15 16 00:01:16,230 --> 00:01:19,520 Remember that programming is just like an open book exam. 16 17 00:01:19,590 --> 00:01:25,170 Everything is out there on the Internet for you to find and you don't have to resort to memorizing these 17 18 00:01:25,170 --> 00:01:26,490 things at all. 18 19 00:01:26,490 --> 00:01:29,580 You just have to know that they exist and roughly how they work. 19 20 00:01:29,820 --> 00:01:35,570 So now let's use this method push to add our you post item into it. 20 21 00:01:35,640 --> 00:01:40,610 At this stage we should at least have one item inside our new post array. 21 22 00:01:41,010 --> 00:01:49,890 Next it's time to use res.redirect in order to send our user back to the home page or the root 22 23 00:01:49,890 --> 00:01:50,680 route. 23 24 00:01:50,880 --> 00:01:58,340 And what this line will do is it will take us right over here and jump into this root route. 24 25 00:01:58,650 --> 00:02:05,910 It's at this stage where we'll console log our posts array so that we see all the posts that are currently 25 26 00:02:05,910 --> 00:02:07,220 stored inside. 26 27 00:02:07,560 --> 00:02:09,620 So now let's give it a spin. 27 28 00:02:09,930 --> 00:02:18,060 If we head over to our compose page and we add some mumbo jumbo in here, hit publish, we get redirect back 28 29 00:02:18,060 --> 00:02:26,250 to the home route and inside our console we get that new post printed and you can see that denoted by 29 30 00:02:26,340 --> 00:02:27,420 the square brackets, 30 31 00:02:27,420 --> 00:02:29,410 this is inside an array. 31 32 00:02:29,430 --> 00:02:36,120 The final thing that I would like to change is we know that this has to be a global variable, 32 33 00:02:36,180 --> 00:02:39,190 it has to be reached by all of these functions. 33 34 00:02:39,240 --> 00:02:45,750 So this is why it's right up at the top and it's outside of any of these other functions. 34 35 00:02:45,750 --> 00:02:49,580 The only thing I would change about this as we mentioned many times before, 35 36 00:02:49,830 --> 00:02:54,120 instead of using a var, you want to go from a let. 36 37 00:02:54,120 --> 00:02:56,790 So a let is a safer version of a var. 37 38 00:02:57,030 --> 00:02:59,330 So this is the solution to challenge 11.