WEBVTT 0 00:00.390 --> 00:03.200 All right, let's go through the answer to this challenge. 1 00:03.360 --> 00:10.290 The steps that we need to perform are we need to create a global container inside our app.js to 2 00:10.290 --> 00:15.260 be able to store each of the posts that come in through this post route 3 00:15.450 --> 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 00:23.700 --> 00:27.470 route for the home page or the root route. 5 00:27.780 --> 00:32.130 And there we're going to console log all of the posts that we've saved. 6 00:32.460 --> 00:39.010 So the first step is creating that global variable. You might have created something using a var 7 00:39.330 --> 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 00:46.740 --> 00:49.850 we're going to set it to an empty array to begin with. 9 00:50.310 --> 00:57.190 So now inside our post route we're able to tap into that posts array, 10 00:57.390 --> 00:58.560 remember the s. 11 00:58.590 --> 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 01:06.540 --> 01:06.790 out 13 01:06.840 --> 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 01:14.220 --> 01:16.210 but you don't have to remember any of them. 15 01:16.230 --> 01:19.520 Remember that programming is just like an open book exam. 16 01:19.590 --> 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 01:25.170 --> 01:26.490 things at all. 18 01:26.490 --> 01:29.580 You just have to know that they exist and roughly how they work. 19 01:29.820 --> 01:35.570 So now let's use this method push to add our you post item into it. 20 01:35.640 --> 01:40.610 At this stage we should at least have one item inside our new post array. 21 01:41.010 --> 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 01:49.890 --> 01:50.680 route. 23 01:50.880 --> 01:58.340 And what this line will do is it will take us right over here and jump into this root route. 24 01:58.650 --> 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 02:05.910 --> 02:07.220 stored inside. 26 02:07.560 --> 02:09.620 So now let's give it a spin. 27 02:09.930 --> 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 02:18.060 --> 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 02:26.340 --> 02:27.420 the square brackets, 30 02:27.420 --> 02:29.410 this is inside an array. 31 02:29.430 --> 02:36.120 The final thing that I would like to change is we know that this has to be a global variable, 32 02:36.180 --> 02:39.190 it has to be reached by all of these functions. 33 02:39.240 --> 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 02:45.750 --> 02:49.580 The only thing I would change about this as we mentioned many times before, 35 02:49.830 --> 02:54.120 instead of using a var, you want to go from a let. 36 02:54.120 --> 02:56.790 So a let is a safer version of a var. 37 02:57.030 --> 02:59.330 So this is the solution to challenge 11.