Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,080 --> 00:00:04,950
Okay, let's now solve this together.
2
00:00:04,950 --> 00:00:07,490
We wanna add a couple of API routes here
3
00:00:07,490 --> 00:00:10,800
so that we can have our own server-side logic
4
00:00:10,800 --> 00:00:14,020
which is detached from rendering pages.
5
00:00:14,020 --> 00:00:15,890
And you'll learn how that works.
6
00:00:15,890 --> 00:00:17,300
In the pages folder
7
00:00:17,300 --> 00:00:22,300
you add that special API folder, which has to be called API.
8
00:00:22,880 --> 00:00:25,070
And in there you can now again create
9
00:00:25,070 --> 00:00:28,040
any file and folder structure of your choice
10
00:00:28,040 --> 00:00:31,570
to define the different API routes and the paths
11
00:00:31,570 --> 00:00:34,840
leading to those routes you wanna use.
12
00:00:34,840 --> 00:00:38,060
For example, we could add a newsletter JS file
13
00:00:38,060 --> 00:00:40,363
for the newsletter related logic.
14
00:00:41,320 --> 00:00:45,220
Then you learn that an API route in the end just is
15
00:00:45,220 --> 00:00:49,490
a function inside of that API route file
16
00:00:49,490 --> 00:00:52,630
which you should export like this.
17
00:00:52,630 --> 00:00:56,660
And this function receives a request and the response object
18
00:00:56,660 --> 00:00:59,720
automatically passed in by next JS
19
00:00:59,720 --> 00:01:03,930
when it executes this function for incoming requests.
20
00:01:03,930 --> 00:01:07,330
Here we can then check the request method, for example
21
00:01:07,330 --> 00:01:10,860
to only proceed for a post requests.
22
00:01:10,860 --> 00:01:14,670
And we can do this here for the email registration
23
00:01:14,670 --> 00:01:17,730
because whilst this is the only kind of method
24
00:01:17,730 --> 00:01:19,140
I wanna support here
25
00:01:19,140 --> 00:01:22,380
I wanna make sure that no other method
26
00:01:22,380 --> 00:01:24,540
triggers any logic here.
27
00:01:24,540 --> 00:01:27,260
So I explicitly check if the incoming request
28
00:01:27,260 --> 00:01:31,310
is a post request and I only wanna proceed in that case
29
00:01:31,310 --> 00:01:34,870
because in that case I for example also know
30
00:01:34,870 --> 00:01:38,620
that I can extract data from the request body.
31
00:01:38,620 --> 00:01:42,883
Because for example, get requests would not carry any data.
32
00:01:43,900 --> 00:01:47,570
So with that, we can extract data from the request body
33
00:01:47,570 --> 00:01:49,960
and we can do it as with the request body field
34
00:01:49,960 --> 00:01:51,360
as you learned.
35
00:01:51,360 --> 00:01:54,970
Here, we could then expect that we have an email field
36
00:01:54,970 --> 00:01:56,610
and that's up to you.
37
00:01:56,610 --> 00:01:59,510
You are the developer of this site.
38
00:01:59,510 --> 00:02:03,190
So it's up to you, how you transmit your data.
39
00:02:03,190 --> 00:02:06,070
And I will therefore expect an email field.
40
00:02:06,070 --> 00:02:08,669
So the user email is then extracted
41
00:02:08,669 --> 00:02:10,252
from the incoming request.
42
00:02:11,310 --> 00:02:13,850
Now here we could then also validate this
43
00:02:13,850 --> 00:02:17,210
and check if it's maybe not valid or
44
00:02:17,210 --> 00:02:18,883
if email,
45
00:02:19,770 --> 00:02:21,130
user email I mean
46
00:02:21,130 --> 00:02:24,930
does maybe not include @ symbol.
47
00:02:24,930 --> 00:02:27,560
Which is a very basic email validation
48
00:02:27,560 --> 00:02:30,630
you might want to implement more (indistinct) one
49
00:02:30,630 --> 00:02:32,070
but it's a start.
50
00:02:32,070 --> 00:02:35,230
And such server-side validation is optional
51
00:02:35,230 --> 00:02:37,350
but strongly recommended.
52
00:02:37,350 --> 00:02:41,400
Because even if you add validation and checks
53
00:02:41,400 --> 00:02:45,120
in your front-end code, those validation steps
54
00:02:45,120 --> 00:02:47,700
can always be circumvented.
55
00:02:47,700 --> 00:02:52,010
After all all your front-end code is exposed to your users
56
00:02:52,010 --> 00:02:54,340
so they could also manipulate it.
57
00:02:54,340 --> 00:02:57,700
So you shouldn't rely on front-end validation
58
00:02:57,700 --> 00:03:01,710
front-end validation is always just a convenience feature
59
00:03:01,710 --> 00:03:03,540
for your users.
60
00:03:03,540 --> 00:03:07,930
Instead to really make sure that you get valid data
61
00:03:07,930 --> 00:03:10,560
and that you work with valid data
62
00:03:10,560 --> 00:03:13,580
you should validate in your API route
63
00:03:13,580 --> 00:03:18,580
because this code can't be viewed or changed by your users.
64
00:03:18,870 --> 00:03:21,790
Now for this task here, it's optional.
65
00:03:21,790 --> 00:03:24,770
I did not tell you to add such validation.
66
00:03:24,770 --> 00:03:28,120
So if you did not do that, that's totally fine.
67
00:03:28,120 --> 00:03:30,140
I just wants to bring it up here
68
00:03:30,140 --> 00:03:33,210
because it is important is to be aware of the role
69
00:03:33,210 --> 00:03:36,200
server-side validation plays.
70
00:03:36,200 --> 00:03:39,460
Now, here we could then return to cancel
71
00:03:39,460 --> 00:03:42,610
the (indistinct) function execution if we make it in here.
72
00:03:42,610 --> 00:03:46,730
So if we have invalid data and we could return a response
73
00:03:46,730 --> 00:03:49,200
with a status code of 422
74
00:03:49,200 --> 00:03:51,700
which is a status code signaling that
75
00:03:51,700 --> 00:03:56,700
the user input was bad, and then maybe set a message of
76
00:03:56,750 --> 00:03:59,080
invalid email address.
77
00:03:59,080 --> 00:04:00,630
Something like this.
78
00:04:00,630 --> 00:04:04,250
And we would send this response back to the user
79
00:04:04,250 --> 00:04:06,430
if the input is invalid.
80
00:04:06,430 --> 00:04:09,900
And there after I return to cancel the function execution
81
00:04:09,900 --> 00:04:13,060
because I will add more code after this if check
82
00:04:13,060 --> 00:04:15,870
and I don't want to execute that code
83
00:04:15,870 --> 00:04:20,089
if we have invalid input, hence the return statement.
84
00:04:20,089 --> 00:04:23,330
Now, if we do make it past this if block though
85
00:04:23,330 --> 00:04:26,270
we know that we do have a valid user input
86
00:04:26,270 --> 00:04:28,240
and then we could store it in a database
87
00:04:28,240 --> 00:04:30,120
or anything like that.
88
00:04:30,120 --> 00:04:31,820
And for the moment we'll not do this
89
00:04:31,820 --> 00:04:35,820
for the moment I'll just console log, that user email.
90
00:04:35,820 --> 00:04:38,920
And that is now a first API route
91
00:04:38,920 --> 00:04:40,610
as we could write it.
92
00:04:40,610 --> 00:04:45,190
An API route for extracting an incoming email address.
93
00:04:45,190 --> 00:04:48,360
Now we wanna send a request to this API route
94
00:04:48,360 --> 00:04:49,650
and we wanna do that
95
00:04:49,650 --> 00:04:52,380
from inside the components input folder
96
00:04:52,380 --> 00:04:55,540
from inside the newsletter registration file.
97
00:04:55,540 --> 00:04:57,990
Here we have the registration handler
98
00:04:57,990 --> 00:05:01,100
which is triggered when the form is submitted.
99
00:05:01,100 --> 00:05:04,060
And here I am preventing the browser default then
100
00:05:04,060 --> 00:05:07,040
to a wide that the page has reloaded.
101
00:05:07,040 --> 00:05:10,740
Now we could also validate the user input here
102
00:05:10,740 --> 00:05:13,520
and show an error message if it is invalid
103
00:05:13,520 --> 00:05:17,850
again, validation here is then just a convenience feature
104
00:05:17,850 --> 00:05:20,340
to show some feedback to the user quickly.
105
00:05:20,340 --> 00:05:22,770
It's not something you should rely on
106
00:05:22,770 --> 00:05:24,810
and it's not something I will add here
107
00:05:24,810 --> 00:05:27,690
because it has nothing to do with API routes
108
00:05:27,690 --> 00:05:30,623
but of course feel free to add it in your solution.
109
00:05:31,930 --> 00:05:34,540
Now, in my case here, I will not add it.
110
00:05:34,540 --> 00:05:38,600
I instead wanna send that HTTP request to the API route
111
00:05:38,600 --> 00:05:40,010
we just added.
112
00:05:40,010 --> 00:05:41,610
And again we can, for example
113
00:05:41,610 --> 00:05:43,330
do this with the fetch function
114
00:05:43,330 --> 00:05:45,023
which is built into the browser.
115
00:05:45,900 --> 00:05:49,920
Now for the URL you'll learned that the API route
116
00:05:49,920 --> 00:05:53,850
is hosted by the same server as our pages are
117
00:05:53,850 --> 00:05:56,550
sent back from so it's the same domain.
118
00:05:56,550 --> 00:05:59,370
And hence can just construct an absolute path
119
00:05:59,370 --> 00:06:04,370
to the API route with /api/newsletter in this case.
120
00:06:05,500 --> 00:06:10,500
/newsletter because I named my API route file, newsletter.
121
00:06:11,250 --> 00:06:13,500
If you named it differently you have to use
122
00:06:13,500 --> 00:06:16,923
a different identifier here in your URL.
123
00:06:18,490 --> 00:06:20,760
Now we wanna send a post request
124
00:06:20,760 --> 00:06:24,200
because we need to add some data to the outgoing requests
125
00:06:24,200 --> 00:06:29,200
and hence here all at this configuration object to fetch
126
00:06:29,300 --> 00:06:33,460
where we can set methods to post and then add a body.
127
00:06:33,460 --> 00:06:37,450
Now, speaking of that body, we need to get access to the
128
00:06:37,450 --> 00:06:39,150
input value here.
129
00:06:39,150 --> 00:06:41,710
And there are two main ways of doing that
130
00:06:41,710 --> 00:06:46,140
we can add state and update the state with every keystroke
131
00:06:46,140 --> 00:06:48,730
or we use a ref and I'll do the letter
132
00:06:48,730 --> 00:06:51,220
since I only need access to the input once
133
00:06:51,220 --> 00:06:52,763
when the form is submitted.
134
00:06:53,760 --> 00:06:55,600
So for this I'll use useRef,
135
00:06:55,600 --> 00:06:57,910
which we need to import from react
136
00:06:58,850 --> 00:07:02,860
and we can create our email inputRef here
137
00:07:02,860 --> 00:07:04,340
like this
138
00:07:04,340 --> 00:07:06,710
and then connect it to the input element.
139
00:07:06,710 --> 00:07:09,720
By adding the special ref prop and pointing at email
140
00:07:09,720 --> 00:07:11,223
inputRef like that.
141
00:07:12,810 --> 00:07:14,320
Now this is connected
142
00:07:14,320 --> 00:07:17,920
and now here we can extract our entered email
143
00:07:17,920 --> 00:07:20,660
inside of this registration handler.
144
00:07:20,660 --> 00:07:22,210
We can get the entered email
145
00:07:22,210 --> 00:07:25,580
by reaching out to the email inputRef.current
146
00:07:25,580 --> 00:07:27,797
because that's how refs work
147
00:07:27,797 --> 00:07:31,510
.value to get the currently entered value.
148
00:07:31,510 --> 00:07:34,460
And that's then what I wanna send to the back-end.
149
00:07:34,460 --> 00:07:38,640
Now we need to send our data as JSON data here.
150
00:07:38,640 --> 00:07:41,600
So call JSON.stringIfy.
151
00:07:41,600 --> 00:07:45,270
And I'll pass in an object where I set a email property
152
00:07:45,270 --> 00:07:47,580
to entered email.
153
00:07:47,580 --> 00:07:50,240
And this should be a email property
154
00:07:50,240 --> 00:07:52,310
because in the API route
155
00:07:52,310 --> 00:07:55,830
I'm extracting a email property on my body.
156
00:07:55,830 --> 00:07:59,550
So I'm looking for a email property in my body.
157
00:07:59,550 --> 00:08:02,370
So if you chose a different identifier here
158
00:08:02,370 --> 00:08:04,310
for extracting the data
159
00:08:04,310 --> 00:08:06,640
you also need to choose a different property
160
00:08:06,640 --> 00:08:08,660
when you send the data.
161
00:08:08,660 --> 00:08:11,593
I'm using email so I'm using it in both places.
162
00:08:12,490 --> 00:08:14,690
And then last but not least, I'll add headers
163
00:08:14,690 --> 00:08:18,350
and set a special content type header like this
164
00:08:18,350 --> 00:08:20,760
and set this to application json
165
00:08:20,760 --> 00:08:24,480
to make the API routes aware of the type of data
166
00:08:24,480 --> 00:08:26,850
my request body will carry.
167
00:08:26,850 --> 00:08:29,920
And that will be json data, because I convert my data
168
00:08:29,920 --> 00:08:33,490
to json with JSON.stringify.
169
00:08:33,490 --> 00:08:36,440
So this sends a request to the API route
170
00:08:36,440 --> 00:08:39,270
and now we can of course handle the response
171
00:08:39,270 --> 00:08:42,070
and then the response data if we want to
172
00:08:42,070 --> 00:08:46,310
so here I'll just get access to the response
173
00:08:46,310 --> 00:08:47,260
data
174
00:08:47,260 --> 00:08:49,560
by calling the json method.
175
00:08:49,560 --> 00:08:52,710
And then here we can console log that data
176
00:08:52,710 --> 00:08:55,163
to just console log the response.
177
00:08:56,050 --> 00:08:57,930
And speaking off the response
178
00:08:57,930 --> 00:09:00,810
there's one thing missing in the API route.
179
00:09:00,810 --> 00:09:02,800
I do send the response here
180
00:09:02,800 --> 00:09:05,560
if we have an invalid email address
181
00:09:05,560 --> 00:09:08,600
but we should absolutely also send the response
182
00:09:08,600 --> 00:09:10,410
if we have a valid one.
183
00:09:10,410 --> 00:09:14,760
So in this scenario, after console logging the entered email
184
00:09:14,760 --> 00:09:18,790
I'll send a response with a status code of 201
185
00:09:18,790 --> 00:09:22,360
which means success, a resource was added.
186
00:09:22,360 --> 00:09:24,100
So the email was stored.
187
00:09:24,100 --> 00:09:27,730
And then we can send back any kind of data,
188
00:09:27,730 --> 00:09:30,520
like for example @json object.
189
00:09:30,520 --> 00:09:33,410
So a JavaScript object converted to json
190
00:09:33,410 --> 00:09:35,920
with help of that json method.
191
00:09:35,920 --> 00:09:39,790
Where we add a message property where we could say
192
00:09:39,790 --> 00:09:41,990
signed up.
193
00:09:41,990 --> 00:09:43,880
Anything like this.
194
00:09:43,880 --> 00:09:45,740
If we do that and save this
195
00:09:47,180 --> 00:09:49,030
we can check if this works.
196
00:09:49,030 --> 00:09:53,720
If we reload our application and open our
197
00:09:53,720 --> 00:09:55,220
developer tools
198
00:09:55,220 --> 00:09:58,200
if I enter something invalid here
199
00:09:58,200 --> 00:10:02,010
I get a warning from my browser, actually,
200
00:10:02,010 --> 00:10:04,320
because this is of type email.
201
00:10:04,320 --> 00:10:07,470
So we have to add something valid here as well.
202
00:10:07,470 --> 00:10:09,780
Though that could be disabled in the browser.
203
00:10:09,780 --> 00:10:11,870
So we should not rely on it.
204
00:10:11,870 --> 00:10:13,900
But if I do enter a valid email address here
205
00:10:13,900 --> 00:10:16,580
and I click register, that's looking good
206
00:10:16,580 --> 00:10:19,700
because I'm printing that response data here
207
00:10:19,700 --> 00:10:21,630
which says signed up.
208
00:10:21,630 --> 00:10:24,740
And then the network tab, we also see that
209
00:10:24,740 --> 00:10:27,340
this request was sent successfully
210
00:10:27,340 --> 00:10:31,340
and that we got this 201 status code back.
211
00:10:31,340 --> 00:10:35,150
And if we have a look at our service side terminal here
212
00:10:35,150 --> 00:10:38,820
so this terminal where we started npm run (indistinct)
213
00:10:38,820 --> 00:10:42,210
where all our service-side logs will be printed
214
00:10:42,210 --> 00:10:45,530
we see that test@test.com is output here
215
00:10:45,530 --> 00:10:48,810
and that makes sense because we are console logging
216
00:10:48,810 --> 00:10:53,780
the arriving email address here in our API route.
217
00:10:53,780 --> 00:10:58,170
So this is now working and you can certainly enhance this
218
00:10:58,170 --> 00:11:02,670
and improve error handling or do anything like that
219
00:11:02,670 --> 00:11:05,370
but this is the core functionality
220
00:11:05,370 --> 00:11:06,710
which you should add here
221
00:11:06,710 --> 00:11:08,443
as your first task.
17346
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.