All language subtitles for 026 Introducing API Routes_en

af Afrikaans
sq Albanian Download
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English Download
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic Download
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish Download
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,070 --> 00:00:04,429 So now we covered getStaticProps 2 00:00:04,429 --> 00:00:07,170 and getStaticPaths and getServerSideProps 3 00:00:07,170 --> 00:00:09,840 and I hope it's clear what these functions do 4 00:00:09,840 --> 00:00:11,450 and why they exist. 5 00:00:11,450 --> 00:00:15,440 They allow us to fetch data for pre-rendering those pages 6 00:00:15,440 --> 00:00:18,280 So that we pre-render the pages with the data 7 00:00:18,280 --> 00:00:21,510 instead of without the data they might need. 8 00:00:21,510 --> 00:00:22,730 Now, up to this point, 9 00:00:22,730 --> 00:00:25,560 we're only working with dummy data though 10 00:00:25,560 --> 00:00:28,780 which is not actually fetched from anywhere. 11 00:00:28,780 --> 00:00:30,740 Instead, we have this dummy array 12 00:00:30,740 --> 00:00:33,359 or in the case of the meetupId page here, 13 00:00:33,359 --> 00:00:35,510 actually hard-coded data 14 00:00:35,510 --> 00:00:37,240 in that return statement. 15 00:00:37,240 --> 00:00:40,510 And that's of course not realistic at all. 16 00:00:40,510 --> 00:00:43,890 Instead, we do have this Add New Meetup page here 17 00:00:43,890 --> 00:00:47,530 which would allow us to enter data for a new meetup. 18 00:00:47,530 --> 00:00:50,680 And hence, we're now going to finish this app 19 00:00:50,680 --> 00:00:52,420 by adding a real backend 20 00:00:52,420 --> 00:00:56,100 with a real database where the data will be stored 21 00:00:56,100 --> 00:00:58,360 and from which we then fetch it. 22 00:00:58,360 --> 00:01:02,650 And this then also allows me to show you the last major 23 00:01:02,650 --> 00:01:04,980 NextJS feature, which is added 24 00:01:04,980 --> 00:01:07,395 by Next to your React apps. 25 00:01:07,395 --> 00:01:11,890 NextJS makes it easy for us to build an API, 26 00:01:11,890 --> 00:01:13,300 a backend API, 27 00:01:13,300 --> 00:01:15,930 together with our front-end React app 28 00:01:15,930 --> 00:01:17,970 in the same project. 29 00:01:17,970 --> 00:01:21,650 For this, we can use another key NextJS feature, 30 00:01:21,650 --> 00:01:24,130 called API routes. 31 00:01:24,130 --> 00:01:28,510 API routes are a special routes, special pages, 32 00:01:28,510 --> 00:01:30,260 if you wanna call it like this 33 00:01:30,260 --> 00:01:34,590 which don't return HTML code, but which are instead 34 00:01:34,590 --> 00:01:37,920 about accepting incoming HTTP requests, 35 00:01:37,920 --> 00:01:40,900 also post patch, put delete requests, 36 00:01:40,900 --> 00:01:45,110 whatever you need with JSON data attached to them 37 00:01:45,110 --> 00:01:48,030 and which then might do whatever you need to do. 38 00:01:48,030 --> 00:01:49,570 For example, store data 39 00:01:49,570 --> 00:01:52,960 in a database and then return JSON data. 40 00:01:52,960 --> 00:01:55,370 So you could say API routes allow you 41 00:01:55,370 --> 00:01:57,990 to build your own API end points 42 00:01:57,990 --> 00:02:00,620 as part of this next project. 43 00:02:00,620 --> 00:02:02,090 And they will then be served 44 00:02:02,090 --> 00:02:05,320 by the same server as your next app. 45 00:02:05,320 --> 00:02:08,580 Now to add API routes, you add a special folder 46 00:02:08,580 --> 00:02:12,840 in your pages folder, and that's a folder named API. 47 00:02:12,840 --> 00:02:16,440 And just as the pages folder has to be named pages, 48 00:02:16,440 --> 00:02:19,160 this folder has to be named API 49 00:02:19,160 --> 00:02:22,210 and it has to be in the pages folder. 50 00:02:22,210 --> 00:02:24,580 Then the NextJS will pick up 51 00:02:24,580 --> 00:02:27,480 any JavaScript files stored in there 52 00:02:27,480 --> 00:02:30,590 and turn those files into API routes. 53 00:02:30,590 --> 00:02:33,690 So into end points, that can be targeted 54 00:02:33,690 --> 00:02:38,350 by requests and that should receive JSON and return JSON 55 00:02:39,620 --> 00:02:41,940 Now in this API folder, you can then again 56 00:02:41,940 --> 00:02:45,250 add JavaScript files where the file names 57 00:02:45,250 --> 00:02:49,240 will act as path segments in the URL. 58 00:02:49,240 --> 00:02:53,553 For example, a new-meetup.js file again, 59 00:02:54,510 --> 00:02:56,670 now here in the API folder. 60 00:02:56,670 --> 00:02:59,720 Now, in those JavaScript files here, 61 00:02:59,720 --> 00:03:03,670 you then don't create a React component function. 62 00:03:03,670 --> 00:03:06,010 These API routes are not 63 00:03:06,010 --> 00:03:10,980 about defining, rendering or returning React components. 64 00:03:10,980 --> 00:03:12,500 Instead in there, 65 00:03:12,500 --> 00:03:16,870 we will define functions which contains server-side code 66 00:03:16,870 --> 00:03:20,240 because API routes will only run on the server 67 00:03:20,240 --> 00:03:21,860 never on the client. 68 00:03:21,860 --> 00:03:25,650 Decoding them will never be exposed to the client. 69 00:03:25,650 --> 00:03:27,460 So we can also use credentials 70 00:03:27,460 --> 00:03:31,040 in API routes without compromising them. 71 00:03:31,040 --> 00:03:34,300 And those functions are then simply triggered 72 00:03:34,300 --> 00:03:37,540 whenever a request is sent to this route, 73 00:03:37,540 --> 00:03:41,530 so to /api/new-meetup here. 74 00:03:41,530 --> 00:03:45,200 This would be the URL of this file 75 00:03:45,200 --> 00:03:47,840 and if a request is sent to this URL, 76 00:03:47,840 --> 00:03:49,510 it will trigger the function 77 00:03:49,510 --> 00:03:51,653 which we have to define in this file. 78 00:03:52,530 --> 00:03:54,670 Now often these function named handler 79 00:03:54,670 --> 00:03:56,140 but the name is up to you, 80 00:03:56,140 --> 00:03:59,080 the important thing is that it's exported. 81 00:03:59,080 --> 00:04:02,720 So we export this handler function here 82 00:04:02,720 --> 00:04:05,410 and this function will receive a request 83 00:04:05,410 --> 00:04:07,250 and a response object. 84 00:04:07,250 --> 00:04:11,640 You might notice from node.js and express.js. 85 00:04:11,640 --> 00:04:14,160 The request object contains data 86 00:04:14,160 --> 00:04:15,990 about the incoming request. 87 00:04:15,990 --> 00:04:18,010 The response object will be needed 88 00:04:18,010 --> 00:04:20,253 for sending back a response. 89 00:04:21,089 --> 00:04:23,010 Now, from that request object, 90 00:04:23,010 --> 00:04:26,650 we can get things like the headers or the request body 91 00:04:26,650 --> 00:04:31,543 and also the request method, route a method property here. 92 00:04:32,700 --> 00:04:36,420 This allows us to find out which kind of request was sent. 93 00:04:36,420 --> 00:04:37,600 And we could, for example, 94 00:04:37,600 --> 00:04:41,730 check if we are receiving having a post request here. 95 00:04:41,730 --> 00:04:44,310 So if the request method is POST, 96 00:04:44,310 --> 00:04:47,430 and we only execute the code in this if check, 97 00:04:47,430 --> 00:04:50,620 if it is a incoming post request. 98 00:04:50,620 --> 00:04:54,360 For other kinds of requests, we don't do anything. 99 00:04:54,360 --> 00:04:57,440 So that would ensure that only post requests 100 00:04:57,440 --> 00:05:01,653 to this route would actually trigger this code in here. 101 00:05:02,870 --> 00:05:07,870 Then here, we can get our data by accessing req.body. 102 00:05:08,040 --> 00:05:10,560 The body field is another built-in field 103 00:05:10,560 --> 00:05:13,350 which contains the body of the incoming request, 104 00:05:13,350 --> 00:05:15,673 the data of the incoming request. 105 00:05:16,770 --> 00:05:19,320 And then we can do whatever we need to do. 106 00:05:19,320 --> 00:05:21,190 Now this year will be the end point 107 00:05:21,190 --> 00:05:23,640 for creating a new meetup. 108 00:05:23,640 --> 00:05:27,940 And therefore it's probably fair to expect that this data 109 00:05:27,940 --> 00:05:32,140 which we get contains a title, a meetup image, 110 00:05:32,140 --> 00:05:35,220 an address and a description field. 111 00:05:35,220 --> 00:05:39,750 After all, it's our page, our project, and our API. 112 00:05:39,750 --> 00:05:42,460 So we can expect whichever data we need. 113 00:05:42,460 --> 00:05:44,720 We will then just have to make sure 114 00:05:44,720 --> 00:05:48,640 that we send the correct data when we do send a request 115 00:05:48,640 --> 00:05:50,860 to this API route later. 116 00:05:50,860 --> 00:05:53,170 But for the moment I do indeed expect 117 00:05:53,170 --> 00:05:56,930 that we get some data out of this data, 118 00:05:56,930 --> 00:05:58,500 so out of this request body 119 00:05:58,500 --> 00:06:00,920 and I'll use object de-structuring here 120 00:06:00,920 --> 00:06:03,740 and I expect to get a title, a image field, 121 00:06:03,740 --> 00:06:06,870 an address field and a description field. 122 00:06:06,870 --> 00:06:09,910 So these are the four fields which I expect to get 123 00:06:09,910 --> 00:06:13,020 on the incoming request body. 124 00:06:13,020 --> 00:06:17,340 And then we can store them in a database, for example, 125 00:06:17,340 --> 00:06:19,240 and that's what we're going to do now. 9723

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.