Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,240 --> 00:00:00,570
All right.
2
00:00:00,570 --> 00:00:03,950
So this was a slightly more difficult challenge
3
00:00:04,130 --> 00:00:05,670
and let's see if you got it right.
4
00:00:06,870 --> 00:00:13,530
Now inside our compose.ejs where we've got our input and our button, in order for us to be able
5
00:00:13,550 --> 00:00:20,440
to pass the value inside our input over to our server where we can log it
6
00:00:20,520 --> 00:00:27,930
we have to include this inside an HTML form so let's add that now. And our input in our button needs
7
00:00:27,930 --> 00:00:35,800
to go inside the form. And we have to make sure that the button is of type submit so that when you click
8
00:00:35,800 --> 00:00:43,070
it it actually submits the data that's inside the form to which ever action is specified here.
9
00:00:43,390 --> 00:00:49,960
Now we need to change this to a route that we're going to handle and I'm going to call it /
10
00:00:50,200 --> 00:00:51,070
compose.
11
00:00:51,220 --> 00:00:56,590
So we handle a post request to the /compose route in our server.
12
00:00:56,950 --> 00:01:01,930
Now the other thing you have to be careful about is your input has to have a name
13
00:01:02,080 --> 00:01:07,200
and this is going to be like the variable name to allow us to tap into its value.
14
00:01:07,360 --> 00:01:12,700
So I'm going to call this the postTitle because that's what we're going to use this input for a little
15
00:01:12,700 --> 00:01:14,390
bit later on. If you called it
16
00:01:14,410 --> 00:01:18,140
anything else that's completely fine as long as you know what it's called
17
00:01:18,310 --> 00:01:22,000
and you know what it refers to then it won't be a problem.
18
00:01:22,030 --> 00:01:24,900
So now that we're done with our compose.
19
00:01:24,940 --> 00:01:31,780
ejs, we're going to have to go into our server and handle what should happen when the page is making
20
00:01:31,780 --> 00:01:35,460
a post requests to the compose route.
21
00:01:35,470 --> 00:01:40,980
Currently all we have is a get method for our compose route.
22
00:01:41,050 --> 00:01:46,420
So our server knows what to do when somebody makes a get request to /compose namely when
23
00:01:46,420 --> 00:01:51,520
they try to view it but the server doesn't yet know what to do when somebody makes a post request to
24
00:01:51,520 --> 00:01:52,120
that route.
25
00:01:52,390 --> 00:02:01,420
So we have to add our app.post method targeting the /compose method and then we add our anonymous
26
00:02:01,420 --> 00:02:08,930
function req and res and then we open up a set of curly braces. Inside
27
00:02:08,930 --> 00:02:16,420
here is where we'll be able to console log the value of the text that was entered into the input over
28
00:02:16,430 --> 00:02:17,040
here.
29
00:02:17,300 --> 00:02:22,500
Now just to remind ourselves, that input had a name of postTitle.
30
00:02:22,700 --> 00:02:27,080
So I need to tap into that inside this route to be able to log it.
31
00:02:27,620 --> 00:02:37,200
And in order to get hold of it I need to use the request.body and this comes from our body-parser
32
00:02:37,200 --> 00:02:39,210
module up here.
33
00:02:39,770 --> 00:02:48,620
So the usual way that we tap into the post request data is we say request.body. the name of the
34
00:02:48,620 --> 00:02:52,990
input which I happened to call postTitle.
35
00:02:53,190 --> 00:03:00,760
Now if we can log the post requests that was sent to /compose and we tap into the body
36
00:03:00,790 --> 00:03:05,710
using body-parser and the value of the variable could postTitle
37
00:03:05,710 --> 00:03:12,790
then once we run this page and refresh it then we should be up to type in some sort of text, hit publish
38
00:03:13,270 --> 00:03:17,800
and head over to our console and see it printed in here.
39
00:03:18,040 --> 00:03:25,840
So how did you get on with that challenge? If it was ok then go ahead and continue to the next challenge.
40
00:03:26,290 --> 00:03:32,200
If it was really difficult or you needed to see all the hints try and review the relevant parts of the
41
00:03:32,200 --> 00:03:33,300
EJS module
42
00:03:33,340 --> 00:03:35,340
where we went through all of this in detail.
4432
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.