YouTube2Text

Learn Node JS in 40 Minutes (Crash Course) - Express, CRUD APIs, etc — Transcript

by RoadsideCoder · 8,137 words · 1,231 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Every back-end job description starts
  2. 0:02with one requirement, Node.js. And by
  3. 0:04the end of this course, you'll build a
  4. 0:06complete job tracker API while learning
  5. 0:08Express, REST APIs, routing, middleware,
  6. 0:12CRUD operations, and everything that you
  7. 0:14need to get started with back-end
  8. 0:15development. So, first of all, what the
  9. 0:18hell is Node.js? So, I have opened this
  10. 0:20browser right here. And as you already
  11. 0:22know, JavaScript is meant to run on
  12. 0:24browsers, right? So, if I write
  13. 0:26console.log,
  14. 0:27and I'm going to say subscribe to
  15. 0:28Roadside Coder and press enter, and
  16. 0:30you'll obviously see that we will get
  17. 0:31the output over here. That's completely
  18. 0:33normal. But, what if we take this code
  19. 0:35right here and go to our VS Code and try
  20. 0:37to run this same code right here? We
  21. 0:39will have to do something like node
  22. 0:41>> [music]
  23. 0:41>> and then the name of the file. So,
  24. 0:42index.js.
  25. 0:44And you can see that this runs. And this
  26. 0:47is made possible using Node.js. And that
  27. 0:49is because Node.js is built on V8 engine
  28. 0:53that Chrome runs on. And that is what
  29. 0:55allows it to run outside of the browser.
  30. 0:57It can read and write files, run
  31. 0:59servers, talk to databases, build CLI
  32. 1:01tools, and much more. And we will cover
  33. 1:03all of these things in this course
  34. 1:05itself. And by the way, guys, I've
  35. 1:06created this complete Node.js cheat
  36. 1:08sheet. So, if you want the link to these
  37. 1:09slides, let me know in the comments down
  38. 1:11below and I'll send you the link. All
  39. 1:12right. Now, first thing's first, we need
  40. 1:13to install Node.js. Yes, Node.js is
  41. 1:16something that you need to install
  42. 1:17inside of your computer. Open this
  43. 1:18nodejs.org website. Click on get
  44. 1:21Node.js. And depending on your platform,
  45. 1:23like for example, if I'm on Windows, it
  46. 1:25shows me Windows installer right here.
  47. 1:27So, I'm going to click on that and just
  48. 1:28save it and you can install it. I
  49. 1:29already installed it, so I don't need to
  50. 1:31install it anymore. And once you have
  51. 1:32done that, you can open any of your
  52. 1:34favorite code editor and just come to
  53. 1:36the terminal and just say node {dash} v,
  54. 1:39and you will see the version of the node
  55. 1:41installed inside of your computer. All
  56. 1:43right. Now, before moving forward, I
  57. 1:44want you guys to understand a very
  58. 1:46important concept of event loop. So,
  59. 1:48see, I have this code right here.
  60. 1:50Console log order. Then we have set
  61. 1:52timeout, which will console log cook, or
  62. 1:55we can also say cooking. And then, we
  63. 1:57will have console log next short. So,
  64. 1:58what do you think is going to be the
  65. 1:59output of this? So, let's try to run it.
  66. 2:02And we say node index.js, and you can
  67. 2:04see it first printed order, then next
  68. 2:07order, and then the cooking at the end.
  69. 2:10And this is because JavaScript does not
  70. 2:12wait for anything. And by the way, this
  71. 2:14is a very important interview question
  72. 2:15as well that I've discussed in my
  73. 2:16interview preparation course as well, so
  74. 2:17you can check that from the link in the
  75. 2:18description down below. So, basically
  76. 2:20JavaScript is a single-threaded
  77. 2:22language. So, it cannot wait for the set
  78. 2:25timeout to get finished, right? Even if
  79. 2:27it's 0 ms, this will be executed at the
  80. 2:30very end. Let me explain you in a better
  81. 2:32way with an example. So, let's say we
  82. 2:33have a chef who is working on a
  83. 2:34restaurant, and he receives an order,
  84. 2:37right? So, that is an instant operation.
  85. 2:39So, console.log order, that will be
  86. 2:40printed. But, he will start cooking that
  87. 2:44particular order in the background,
  88. 2:46right? And meanwhile, waiter will go on
  89. 2:49and bring another order. So, that order
  90. 2:51will be printed instantly. The chef will
  91. 2:53not wait for the previous
  92. 2:55order to be cooked and then receive the
  93. 2:57next order, right? He can start working
  94. 2:59on those orders asynchronously, right?
  95. 3:01So, but as soon as that particular order
  96. 3:03is cooked, we can console.log cook
  97. 3:05operation as well, right? So, similarly,
  98. 3:08JavaScript works the same way. It works
  99. 3:11on the synchronous actions first and
  100. 3:13then picks up the asynchronous operation
  101. 3:15at the very end. So, you can see the
  102. 3:17cooking was printed at the very end.
  103. 3:19Okay. So, now how do we go on and create
  104. 3:22our very first Node.js project? We will
  105. 3:25use something called as NPM, Node
  106. 3:27Package Manager. And Node.js comes
  107. 3:29bundled with this NPM. So, I'm going to
  108. 3:32go to my terminal, and I'm going to say
  109. 3:33NPM init, and press enter. And it's
  110. 3:36going to be asking me the name of my
  111. 3:38package. So, I'm going to press enter.
  112. 3:39Let's keep it this.
  113. 3:41Version one, description if you want you
  114. 3:43can enter it. Entry point can be
  115. 3:44index.js. Yes, we have already created
  116. 3:46it. Test command, we don't need to do
  117. 3:48that. Get repository, keywords, we can
  118. 3:50keep all of these empty for now. Now,
  119. 3:52type is something that is very
  120. 3:53important. So, we will keep it common js
  121. 3:55for this video to keep it very simple
  122. 3:57and you'll notice that this will
  123. 3:58generate something called as a
  124. 4:00package.json
  125. 4:02file right here, right? With all of
  126. 4:03these details that you have entered.
  127. 4:05Now, whenever we install a new package,
  128. 4:07it will show up right here. Like for
  129. 4:09example, I'm going to say npm install
  130. 4:11express. Now, express is basically a
  131. 4:14framework which is built on top of node
  132. 4:16js. We will explore that as well later
  133. 4:18in this video. So, you will see that we
  134. 4:20will have this express as a part of
  135. 4:22dependencies right here. We will also be
  136. 4:25installing something called as nodemon,
  137. 4:28which is going to be used to restart our
  138. 4:30server basically whenever we make any
  139. 4:32changes inside of the index.js file. So,
  140. 4:35that we don't have to do, you know, node
  141. 4:37index.js again and again. So, see, what
  142. 4:39I'll do right here in the scripts, you
  143. 4:42don't need this test script. I will be
  144. 4:44adding a dev script over here and I'll
  145. 4:46say nodemon and you can write the name
  146. 4:49of your file. index.js
  147. 4:51right here. Let me just run it. So, I'll
  148. 4:53say npm run dev. There we go. It is
  149. 4:56running. Now, if I go to index.js, let's
  150. 4:58bring back that code. If I just save it,
  151. 5:00now you can see it has restarted our
  152. 5:03server and it has printed this file,
  153. 5:05right? So, this is super helpful. Cool.
  154. 5:06Now, let's go on and understand some of
  155. 5:08the core modules of node js. So, first
  156. 5:10of all, let's discuss the fs and path
  157. 5:13modules. So, fs basically allows us to
  158. 5:15read and write files using node js and
  159. 5:18path basically helps us generate the
  160. 5:20path to that particular file
  161. 5:22that we're trying to read and write. Let
  162. 5:23me show you. So, let's create a new file
  163. 5:25right here called data.json
  164. 5:28and I'm going to put some dummy data
  165. 5:30inside of it. If you want, you can do
  166. 5:31that as well. Let's just make this data
  167. 5:33smaller so you guys can copy it up if
  168. 5:35you want. So, I've put this data over
  169. 5:38here and now I'm going to go back to
  170. 5:39index.js
  171. 5:41and I'll first of all import the fs
  172. 5:42modules. So, while we are we were
  173. 5:44creating the app, there was option to
  174. 5:45select uh common js, right? So,
  175. 5:47basically there are two ways to import
  176. 5:49something in Node.js. So, you can do
  177. 5:52const fs
  178. 5:54= require, and then the name of the
  179. 5:57package. So, we have installed two
  180. 5:58packages right here, Express and
  181. 6:00Nodemon. So, we can choose that.
  182. 6:02Otherwise, we can choose the inbuilt fs
  183. 6:04package inside of Node.js. And if you
  184. 6:06want to import it something like this,
  185. 6:09import fs from
  186. 6:12fs, which won't work right now. So, you
  187. 6:13can see this is crashing. All we have to
  188. 6:15do is change this common JS to module
  189. 6:18right here, right? But to keep things
  190. 6:20simple, as I mentioned, we will use
  191. 6:22common JS in this video.
  192. 6:24Let's get rid of this. We will also be
  193. 6:26importing the path module inside of this
  194. 6:29file from Node.js. And now, let's get
  195. 6:31the path to this data.json file. So, we
  196. 6:34will say path
  197. 6:36.join.
  198. 6:38And here, we want to first full target
  199. 6:40the current directory. So, it will be
  200. 6:42__dirname,
  201. 6:45and then the name of the file that we
  202. 6:46want to reach. So, this path to some
  203. 6:49variable like file path.
  204. 6:54Right? Now, if you want to read the
  205. 6:56contents inside of this data.json file,
  206. 6:58we have two ways by using this fs. First
  207. 7:01is the synchronous way, which, you know,
  208. 7:03blocks the thread and the flow of our
  209. 7:05code. And second one is asynchronous,
  210. 7:07which is generally preferred in the real
  211. 7:09apps. So, I'll say fs
  212. 7:11.readFileSync.
  213. 7:13That is, read file synchronous. I'll
  214. 7:15provide it the file path that we have
  215. 7:17taken. So, you can see we are providing
  216. 7:19the path, and then it takes the options
  217. 7:21right here.
  218. 7:23So, I'm going to provide UTF-8.
  219. 7:25So, this will return us the content of
  220. 7:28our file. So, I'll just save it inside
  221. 7:29of this data variable. Okay, let's see.
  222. 7:33How about we go on and console log this?
  223. 7:34So, I'll just say console log
  224. 7:37this data over here, and let's go and
  225. 7:39check our terminal. Yep, you can see
  226. 7:42this data right here. Awesome. Now,
  227. 7:44let's try doing this again, but in a
  228. 7:47asynchronous way. So, instead of read
  229. 7:49file sync,
  230. 7:51I'll simply say read file right here.
  231. 7:54We'll provide the file path, UTF-8 type,
  232. 7:56and then we can also provide it with a
  233. 7:59callback right here. We will get the
  234. 8:01error over here if there's any error,
  235. 8:03and the data that we will get from it.
  236. 8:05And actually, we don't need this
  237. 8:07constant data now. So, if there's any
  238. 8:09error, we can throw that error right
  239. 8:10here. Otherwise, we can simply console
  240. 8:12log the data. So, I'll just comment this
  241. 8:14out. Let's save this again. And yep, we
  242. 8:16can see right here. Awesome. Like let's
  243. 8:19change the name of this file, and you
  244. 8:21will see that we get this error right
  245. 8:23here, right? Because we have checked if
  246. 8:25there's any error, then throw the error.
  247. 8:27Awesome. So, this asynchronous version
  248. 8:29basically exists for scripting and
  249. 8:31tooling, but in a real server, you will
  250. 8:33want to prefer this asynchronous method
  251. 8:35so that you don't block the other
  252. 8:37requests. All right. Now, how do you go
  253. 8:39on and write inside of a file, right?
  254. 8:41This one is for reading, but how do you
  255. 8:43write it? So, I'll just get rid of all
  256. 8:45of this code right here, and I'll take a
  257. 8:47new data over here, right? With this
  258. 8:49application, this application, like
  259. 8:51three applications right here. I'll just
  260. 8:52shrink it right here. Now, we want to
  261. 8:54replace this file data with our new
  262. 8:57data. So, to write inside of this file,
  263. 8:59I'll say fs. write
  264. 9:02file. And this also has write file
  265. 9:04asynchronous and synchronous. Again, we
  266. 9:06will prefer the asynchronous version
  267. 9:08over here. I'll provide it with the file
  268. 9:10path, and then I'll provide it with the
  269. 9:12data. So, I'll say JSON.stringify
  270. 9:14to convert the object into a string.
  271. 9:17I'll provide it with the new data, and
  272. 9:19you'll see it takes the value first. If
  273. 9:21there's any replacer, right? Like key or
  274. 9:24value, anything like that. This is
  275. 9:25basically a callback, so I'll just say
  276. 9:26null over here, and then the space, so
  277. 9:29I'll just put two over here. See, you
  278. 9:31can read all of this description right
  279. 9:33here in the VS Code. Adds indentation,
  280. 9:34white space, and line break characters
  281. 9:36to return the value JSON text. Okay,
  282. 9:38cool. And after that, we can have a
  283. 9:41callback function right here. We'll take
  284. 9:43the error if there's any error, we are
  285. 9:45going to print that error. Otherwise,
  286. 9:46we're going to simply going to console
  287. 9:47log asynchronous write complete. And I
  288. 9:50think it should have done because oops,
  289. 9:52it's starting again and again. Why don't
  290. 9:54we just use uh
  291. 9:55node index.js right here. So, you can
  292. 9:57see async write complete. Let's go to
  293. 9:59the data.json. You can see all of these
  294. 10:02three objects are right here. Right.
  295. 10:04Now, let's go on and create our very
  296. 10:06first server in Node.js. So, to create a
  297. 10:09web server in Node.js, you have to
  298. 10:10import something called as an HTTP,
  299. 10:14which comes from Node.js. And I'll say
  300. 10:16const server equals http. create
  301. 10:21server. And this basically takes a
  302. 10:23callback right here with request and
  303. 10:27response. And inside of this, we can
  304. 10:29define our different different
  305. 10:30endpoints. So, I'll say if request.url.
  306. 10:34So, this basically this request object
  307. 10:35provides us a bunch of different things.
  308. 10:37One of them is URL. So, I'll say if URL,
  309. 10:39if it's equals to slash, that is just
  310. 10:41simply localhost or whatever the
  311. 10:43endpoint of your API is, and I'll say
  312. 10:45and
  313. 10:46request.method.
  314. 10:48So, basically there are four types of
  315. 10:50method: get, post, put, and delete. Get
  316. 10:53is for reading, post is for creating
  317. 10:55something, put is for updating
  318. 10:57something, and delete is obviously for
  319. 10:59deleting something. Right? So, this one
  320. 11:01will be a get request. So, if that is
  321. 11:03the case, I'll send some response over
  322. 11:05here. Right? So, I'll say response.write
  323. 11:09head. So, this sends a response header
  324. 11:12to the request. I'll say 200. Now, there
  325. 11:14are a bunch of different different types
  326. 11:15of request code over here. You can just
  327. 11:18Google HTTP response codes, and you will
  328. 11:21land on this you know MDN website. So,
  329. 11:23for informational responses, hundreds,
  330. 11:25successful responses from 200 to 299,
  331. 11:28for client errors, 400, server errors,
  332. 11:30500. So, we have different different
  333. 11:32types of responses. And if I click on
  334. 11:34the successful responses, you'll find
  335. 11:36each and every code right here. So, I
  336. 11:38would highly encourage you guys to go
  337. 11:39through this. We are going to be sending
  338. 11:41the success code right here, 200. And
  339. 11:43content type will be text/plain. And
  340. 11:46then I'll say response.end, "Welcome to
  341. 11:48Job Tracker API." Right? How about we go
  342. 11:51on and try to run this? So, I'll just
  343. 11:52run npm run dev. Okay, our API is
  344. 11:56started. And actually, we need to serve
  345. 11:58it in some sort of uh endpoint, right?
  346. 12:01So, I'll say server.
  347. 12:03listen. I'll choose the port to be 3000.
  348. 12:07And here in the callback, I'll just
  349. 12:09simply say console.log, "Server running
  350. 12:11on localhost:3000." So, see. It is
  351. 12:13printing it right here. Let's go to this
  352. 12:14endpoint. And there we go. We can see,
  353. 12:16"Welcome to Job Tracker API."
  354. 12:19Awesome. Let's create another type of
  355. 12:21endpoint. So, I'll say else if What if
  356. 12:25uh the endpoint is something like
  357. 12:26{slash}health? So, request.url equals
  358. 12:28{slash}health and request.method is
  359. 12:30equals to get. Then, I'll just say 200
  360. 12:34code with application/json. And I'll
  361. 12:36simply send the status to be okay. Let's
  362. 12:38try to hit this endpoint. So, I'll say
  363. 12:40health. Press enter. There we go. The
  364. 12:43status is okay. Awesome. And if it is
  365. 12:45none of these things, I'll just simply
  366. 12:47say response.writeHead 404, that is not
  367. 12:50found. 404 code.
  368. 12:52And content type will be text/plain. And
  369. 12:55response will be not found. Let's try to
  370. 12:58enter some ran- something random over
  371. 12:59here. And you can see, "Not found."
  372. 13:01Cool. So, you have successfully created
  373. 13:03your very first web server or your very
  374. 13:06first API endpoint. But you can see,
  375. 13:08this feels a little bit tedious, right?
  376. 13:11Imagine writing else if else if for I
  377. 13:14don't know, 50 different API endpoints
  378. 13:16or even 15 different API endpoints. It
  379. 13:19will be an absolute nightmare. And that
  380. 13:21is why the modern apps use something
  381. 13:23called as Express. Right? So, you can
  382. 13:26see, raw Node JS is like built from
  383. 13:28bricks, but Express is made with
  384. 13:31pre-built walls. It has modules that are
  385. 13:33ready to use and you can stick them
  386. 13:35together to create a web server in much
  387. 13:37more easier way and create your APIs in
  388. 13:39much faster time. Let me actually show
  389. 13:41you. We already installed Express,
  390. 13:42right? So, instead of all of this now,
  391. 13:46we are going to be importing
  392. 13:48Express right here. Assign it to
  393. 13:50Express. I'll say const app equals
  394. 13:53Express right here. Now, we can simply
  395. 13:56say app. Whatever the request that we're
  396. 13:59trying to make, get, post, put, delete,
  397. 14:01whatever, right? So, I'll just say get
  398. 14:03and I'll say {slash} and I'll have a
  399. 14:06callback right here. Again, this will
  400. 14:08take request and response, but inside of
  401. 14:11this curly braces, it has handled all of
  402. 14:14the code and content type stuff and we
  403. 14:16can simply just send the response. I'll
  404. 14:17say response .send welcome to job
  405. 14:19tracker API. And then, if you want to
  406. 14:21listen on a particular port, I can say
  407. 14:22app.listen
  408. 14:24port 3000. And inside of callback, I'm
  409. 14:27going to write console.log server
  410. 14:29running on port 3000. Let's see inside
  411. 14:31of our terminal. Yep, that is fine.
  412. 14:33Let's go and check the localhost 3000.
  413. 14:35Yes, awesome. This is working
  414. 14:37flawlessly. So, earlier we had so much
  415. 14:40code and now it has been simplified to
  416. 14:43just a few lines. And again, guys, if
  417. 14:44you want the link to this complete cheat
  418. 14:46sheet, let me know in the comments down
  419. 14:47below. Now, similar to this route that
  420. 14:50we have created right here, we can
  421. 14:51create different different types of
  422. 14:52routes.
  423. 14:53Like one of the examples that I would
  424. 14:54like to give you is uh if I just copy
  425. 14:56and paste this right here. If we want to
  426. 14:58go to {slash} application and a
  427. 15:00particular ID over here, right? So, we
  428. 15:02don't know that ID. So, we're going to
  429. 15:04be replacing it with this colon ID over
  430. 15:06here, so that this becomes a dynamic
  431. 15:09route. Now, simply over here, we can say
  432. 15:11inside of backticks, actually, fetching
  433. 15:14application with ID and the particular
  434. 15:16ID that we're trying to search, right?
  435. 15:17So, let's try to go to this the {slash}
  436. 15:19applic-
  437. 15:21-ations {slash} let's just say ABC over
  438. 15:24here. And you can see fetching
  439. 15:25application with ID ABC. Now, you guys
  440. 15:28may have also seen in our URLs, we have
  441. 15:31something like this. So, for example,
  442. 15:32{slash} application and {question mark}
  443. 15:34status
  444. 15:36equals, let's say, applied. Right? So,
  445. 15:39how does this work? How does
  446. 15:41these search param work? Currently, it
  447. 15:43will just say cannot get. So, again, I'm
  448. 15:45going to duplicate this right here, and
  449. 15:48I'll just remove this to keep it {slash}
  450. 15:50applications. And inside of it,
  451. 15:52actually, we get something called as
  452. 15:53request. {dot} query. And from inside of
  453. 15:57this, we are going to destructure
  454. 15:59whatever that we want to get, right? So,
  455. 16:01status in this case. And let's just
  456. 16:03console log filtering with filtering by
  457. 16:06status and whatever the status is.
  458. 16:08Let's see. If I just refresh this now,
  459. 16:10you can see filtering by status applied.
  460. 16:13Awesome. Now, there's something called
  461. 16:14as a middleware when we use Express.
  462. 16:17Whenever we make a request inside of our
  463. 16:20URL, it goes through that particular
  464. 16:22middleware before providing us with
  465. 16:24response. And it looks something like
  466. 16:25this. So, app.use, and we can perform a
  467. 16:28particular action in between. Let me
  468. 16:29show you how. So, right here, right
  469. 16:32after this, actually,
  470. 16:34I'm going to say app.use
  471. 16:37express
  472. 16:39.json. Now, this will allow me to return
  473. 16:42the middleware that only parses JSON and
  474. 16:44only looks at the request where the
  475. 16:47content type matches the JSON type. So,
  476. 16:49we don't have to check it again and
  477. 16:51again, right? And let's create another
  478. 16:53middleware, app.use, and I'll take a
  479. 16:56callback right here with request,
  480. 16:58response, and something new called next,
  481. 17:00so that we can allow it to go to the
  482. 17:02next step. I'll simply console log
  483. 17:04request.method right here and
  484. 17:06request.URL, and I'm going to call this
  485. 17:09next over here, so that it moves
  486. 17:11forward.
  487. 17:12Okay. Let's try to refresh this, and
  488. 17:15let's go back to our terminal,
  489. 17:17and you will see inside of our terminal
  490. 17:18we have the request.method to be get and
  491. 17:21request.url to be /applications.
  492. 17:24And keep in mind if you forgot to add
  493. 17:26this next over here, then your app will
  494. 17:29be stuck forever in this place and it
  495. 17:31will completely, you know, be unusable.
  496. 17:33So, this is the number one beginner
  497. 17:34mistakes mistake that a lot of people
  498. 17:37make. Okay. Now, before moving on to
  499. 17:39creating our complete job tracker API,
  500. 17:41let's also understand how we can handle
  501. 17:44errors using middleware. So, I'll just
  502. 17:47say app.use and this inside of this the
  503. 17:50callback also takes the error, something
  504. 17:54like this. So, error, request, response,
  505. 17:56next.
  506. 17:57Inside of this, I'll just print whatever
  507. 17:59the error is over here and we can also
  508. 18:01say response.status
  509. 18:03to be with respect to that error we can
  510. 18:04throw the response, right? So,
  511. 18:06response.status 500 and something went
  512. 18:08wrong. Let's actually take this and put
  513. 18:10it right before app.listen. This is
  514. 18:12where we, you know, usually put all of
  515. 18:14these errors. And if I refresh my app
  516. 18:16now, nothing happens. It's because this
  517. 18:18will, you know, handle if there's any
  518. 18:20actual error inside of our app and right
  519. 18:22now there isn't. All right. Now, finally
  520. 18:24we are going to be working on our full
  521. 18:25CRUD API. We're going to be creating all
  522. 18:27of these different different routes by
  523. 18:29building this job application tracker
  524. 18:31API with the following features. So, let
  525. 18:34me actually show you what are all the
  526. 18:35different different endpoints going to
  527. 18:36be. So, for example, if you want to list
  528. 18:38all of the applications inside of our
  529. 18:40app, we can just provide it with the
  530. 18:42following things like applied,
  531. 18:44interview, offer, rejected, whatever the
  532. 18:47current status is. You can even sort it
  533. 18:50using date. I have even added pagination
  534. 18:53inside of it and I'll show you how you
  535. 18:55can build pagination with these types of
  536. 18:56API endpoint. If I click on execute,
  537. 18:59you'll see that we will get our response
  538. 19:02just like this. Currently, I've just
  539. 19:03added one job application in there,
  540. 19:05that's why. But, we can also create new
  541. 19:08applications over here in the
  542. 19:09/applications. You can see this is the
  543. 19:11type of a schema that you can use right
  544. 19:13here. So, if I click on try it out and
  545. 19:15let me just change it to something like
  546. 19:17meta and click on execute now. Okay, it
  547. 19:20has been created, but if I go back up,
  548. 19:23well, let's try to hit this get endpoint
  549. 19:25again and this time I'll try to let's
  550. 19:27say add five results even though we just
  551. 19:29have two in there. So, I click on
  552. 19:31execute. Yep, there we go. We can see
  553. 19:33the newly added one over here as well.
  554. 19:35Let's take this ID and let's make use of
  555. 19:38this slash application slash ID. So, if
  556. 19:40I
  557. 19:41click on try out,
  558. 19:43I'll just say two over here, execute and
  559. 19:44we can get that specific job application
  560. 19:48over here. We also have stats over here.
  561. 19:51If I click on try it out, you'll notice
  562. 19:53that this will give us different
  563. 19:55different stats like up how many have we
  564. 19:56applied on, how many were reject
  565. 19:59rejected, interview, etc. Right?
  566. 20:00Currently, we just have the applied,
  567. 20:02that's why it's just showing this. We
  568. 20:04also have other endpoint like update an
  569. 20:06application, delete an application,
  570. 20:09right? So, all of these is what we're
  571. 20:11going to be building next. All right, so
  572. 20:13up until this point, we were writing all
  573. 20:15of our code inside of this one index.js
  574. 20:17file, right? But,
  575. 20:19in the production apps, we generally
  576. 20:21write our code in different different
  577. 20:23routes and controllers files, right? So,
  578. 20:26first of all, let me just rename this
  579. 20:27file to app.js and inside of
  580. 20:28package.json, I'm going to be
  581. 20:31calling it app.js, right? So, let me
  582. 20:34show you. See, something like this. We
  583. 20:35will create the routes folder for
  584. 20:38defining all of our different different
  585. 20:40routes and the controllers which will
  586. 20:42basically contain the core logic of our
  587. 20:45APIs. So, let's create both of these
  588. 20:47folders right here. New folder, routes
  589. 20:50and I'm going to create a new folder for
  590. 20:52controllers.
  591. 20:53Okay. Now, inside of this app.js, let's
  592. 20:56first create our web servers just the
  593. 20:58way that we learned, right? So, I'll
  594. 21:00import express first. So, const express
  595. 21:02equals require express. After that,
  596. 21:04obviously, we are going to be taking or
  597. 21:06initializing our app with Express so
  598. 21:09that we can create our server and also
  599. 21:11we will add the middleware by saying we
  600. 21:13should return middleware that only
  601. 21:14parses JSON and only looks at requests
  602. 21:17where content type matches this option.
  603. 21:19And whenever we hit any endpoint, what I
  604. 21:22want is right here inside of our
  605. 21:24terminal, we want to see
  606. 21:26which endpoint we hit. So, I'll add one
  607. 21:28more middleware over here. Request,
  608. 21:30response, and next.
  609. 21:31I'll say console.log request method and
  610. 21:33request URL, right? And I'm going to say
  611. 21:35next. Now, as I showed you to create a
  612. 21:37server, we say app.listen.
  613. 21:40We're going to be listening on 3000
  614. 21:42port. And inside of this callback, I'll
  615. 21:44say job tracker API running on this
  616. 21:46localhost 3000. Okay, that's fine. For
  617. 21:49our
  618. 21:50simple uh slash route, we're not going
  619. 21:52to be having anything. So, I'll just
  620. 21:53simply say welcome to the job tracker
  621. 21:55API. Let's just quickly test it out to
  622. 21:57see if this is run working. Yep, there
  623. 22:00we go. Welcome to job tracker API. It is
  624. 22:02working fine. Now, for our main routes
  625. 22:05like {slash} application. So, what I'll
  626. 22:07do I'll
  627. 22:08uh just I'm going to copy and paste it
  628. 22:09right here and I'll remove this from
  629. 22:12over here.
  630. 22:13So, it'll be {slash} appli-
  631. 22:15cations. What this will do, it will
  632. 22:17fetch all of this data from this
  633. 22:18data.json file. And by the way, for this
  634. 22:21tutorial, we're not taking any database
  635. 22:22to keep things simple and so that you
  636. 22:24guys are able to understand Node and
  637. 22:25Express, we are going to be writing
  638. 22:28our data, reading the data, you know,
  639. 22:30creating, deleting, updating all of them
  640. 22:32inside of this JSON file. Let me know in
  641. 22:34the comments down below if you want me
  642. 22:35to create a much more in-depth Node.js
  643. 22:37tutorial where we build a complete
  644. 22:40end-to-end API with database, auth, and
  645. 22:42all of those things, right? For this
  646. 22:43tutorial, the scope is mainly to get you
  647. 22:45started with Node.js. Okay. So, over
  648. 22:47here, we will define our route
  649. 22:49basically. Let's call it applications
  650. 22:50router. And obviously, we will get the
  651. 22:52error because this does not exist. So,
  652. 22:54inside of the routes folder over here,
  653. 22:56I'll create a new file called
  654. 22:58applications
  655. 23:00.routes
  656. 23:02.js. And right here, first of all, I'm
  657. 23:04going to be importing Express, and then
  658. 23:06I'll say const router equals Express dot
  659. 23:10router. So, over here, basically, router
  660. 23:13will allow us to create the sub routes
  661. 23:15under a particular route. So, we have
  662. 23:18{slash} applications, right? So, now, if
  663. 23:21we say {slash} over here and create some
  664. 23:23logic, it means it will be {slash}
  665. 23:25application. If we say {slash} ID, it
  666. 23:27will be {slash} applications {slash} ID.
  667. 23:29Let me
  668. 23:30So, over here, first of all, we are
  669. 23:32going to be creating
  670. 23:33the get all applications logic. For
  671. 23:36that, I'm going to say router dot and
  672. 23:38we're going to be providing the method,
  673. 23:39if it's get, post, put, etc., right? So,
  674. 23:41this will be get. And I'll say {slash}
  675. 23:44over here. Then, comma, we are going to
  676. 23:45be adding the logic corresponding to
  677. 23:48that, right? And in the end, we're going
  678. 23:49to say module.exports
  679. 23:52equals router.
  680. 23:54And then, we can simply go to our app.js
  681. 23:57and import it
  682. 23:58right here. Something like this. So,
  683. 24:00applications router, require, and then
  684. 24:02the path to that particular file. Now,
  685. 24:04our app won't give us any errors. Okay,
  686. 24:06so, apart from that, what else we had?
  687. 24:09We want to get application counts by
  688. 24:11status, we want to get single
  689. 24:13application by the ID. Now, one thing
  690. 24:16very important to understand right here
  691. 24:17is the way that you write your get
  692. 24:20routes matter a lot. So, if you will
  693. 24:23write this {colon} ID first and this
  694. 24:26{slash} stats later on, Express will
  695. 24:28think that this stats is also an ID, and
  696. 24:31it will, you know, correspond it with
  697. 24:32this particular route only, because it
  698. 24:34reads from top to bottom. So, that is
  699. 24:36why if you have any {colon} ID or
  700. 24:38something like that, it is
  701. 24:39important to write it below the {slash}
  702. 24:42whatever the route is. Let me show you,
  703. 24:45actually. I'm going to duplicate this
  704. 24:47over here and here as well. This one
  705. 24:50will be stats, and this one will be
  706. 24:52{colon} ID. And obviously, we are going
  707. 24:54to be writing the logic for each and
  708. 24:56every one of these. So, let's actually
  709. 24:58create all of these get APIs over here.
  710. 25:01And again, as I mentioned, we are going
  711. 25:02to be manipulating the data inside of
  712. 25:04it. So, instead of our controllers
  713. 25:06folder, I'm going to be creating a new
  714. 25:08file applications.controller.js.
  715. 25:12And for each route over here, like for
  716. 25:14example, to get all applications, we are
  717. 25:16going to be creating different different
  718. 25:17controllers. Like for example,
  719. 25:19exports.get
  720. 25:20all applications.
  721. 25:22And this will be a function just like
  722. 25:24this. And how would you import it? So,
  723. 25:26I'll go right here instead of this. I'll
  724. 25:29write this. And you can import it
  725. 25:31something like this.
  726. 25:33Or you could have also done controller
  727. 25:36over here and done something like
  728. 25:37controller.get
  729. 25:39all applications. I'm going to be doing
  730. 25:41the previous one where I destructured
  731. 25:43the controller right here. This is much
  732. 25:44more cleaner. Now, before writing the
  733. 25:46logic for each and every one of these
  734. 25:48controllers, let's do one thing. Let's
  735. 25:50complete our app.js file. Below this,
  736. 25:53I'm going to say app.use. And I'm going
  737. 25:55to check if a route does not exist. User
  738. 25:57is trying to hit a route that does not
  739. 25:59exist. I'll say request, response.
  740. 26:03Below it, I'll just say response.status.
  741. 26:05And we can provide the code over here as
  742. 26:07well. So, 404 not found. I'll say dot
  743. 26:10JSON. And I'll provide the response over
  744. 26:12here. Success to be false and error
  745. 26:14route not found. After that, we will
  746. 26:16also do error handling. So, app.use.
  747. 26:19And here, as I showed you, we are going
  748. 26:21to be having error, request, response,
  749. 26:23next. And inside of this, we can simply
  750. 26:25just say console error, whatever the
  751. 26:27error is.
  752. 26:28And re-
  753. 26:29response.status 500. That is server
  754. 26:32error. Dot JSON. Success to be false and
  755. 26:34error internal server error. Cool.
  756. 26:37Now, let's go back to our
  757. 26:38applications.controller.
  758. 26:40We are going to be reading the data from
  759. 26:42this data.json, right? And again, as I
  760. 26:44mentioned, you guys can use a DB if you
  761. 26:46want over here as well. But for the
  762. 26:48simplicity of this tutorial, you can
  763. 26:50follow along and read the data from this
  764. 26:52file right here. So, let me show you how
  765. 26:53we can do that. So, obviously, we are
  766. 26:55going to be using these two modules, fs
  767. 26:57for reading the files or reading or
  768. 26:59writing the files, and path for getting
  769. 27:01the path to that particular file. So,
  770. 27:03I'll say const data, oops, data file, or
  771. 27:07you can also call it DB or whatever,
  772. 27:08right? path.join,
  773. 27:11and I'll say current path, so underscore
  774. 27:13underscore dirname, comma, and I'll say
  775. 27:16dot dot slash. We want to go to this
  776. 27:19file, right? data. json to dot dot
  777. 27:20slash, that is one folder back,
  778. 27:23data.json.
  779. 27:24Okay. Now, I'm going to be creating a
  780. 27:26couple of helper functions over here.
  781. 27:28One is for reading the data, and one is
  782. 27:30for writing the data. So,
  783. 27:32function read data first. I'll say const
  784. 27:35raw equals fs. I'll say read
  785. 27:40file. We are going to be making the
  786. 27:41asynchronous call to reading the file,
  787. 27:43right? I'll say data file, comma,
  788. 27:47UTF
  789. 27:488 type. And then, simply, we are going
  790. 27:50to return the JSON.parse.
  791. 27:53We're going to be parsing this data, and
  792. 27:55we're going to be taking out the
  793. 27:56applications
  794. 27:58from inside of it. So, let me show you
  795. 27:59actually
  796. 28:00this data.json file. We have this
  797. 28:03applications over here, right? So, this
  798. 28:05is what we want as a response to our
  799. 28:07front end, to send to our front end.
  800. 28:09Now, we are going to say write data. So,
  801. 28:11we will get the applications data that
  802. 28:13we're supposed to write inside of this
  803. 28:15data.json file. So, I'll say fs.write
  804. 28:19file synchronously, or you can also use
  805. 28:22asynchronously, as well.
  806. 28:24We can use that. And I'll say data
  807. 28:26file, JSON.stringify.
  808. 28:29Here, I'll provide the applications that
  809. 28:31we are sending. Next parameter will be
  810. 28:34null. We don't want to transform it, and
  811. 28:36I'll say
  812. 28:37two for indentation and white spaces.
  813. 28:40Okay, cool. So, these are all the helper
  814. 28:42functions that we are going to be
  815. 28:43needing. Let's start
  816. 28:45working on this controller now. So,
  817. 28:47inside of this, first of all, I'm going
  818. 28:48to say request.query.
  819. 28:51So, if you remember, when we are getting
  820. 28:54our applications, we have multiple
  821. 28:55different things that we are sorting it
  822. 28:57by, like applied, what page we are on,
  823. 29:00how many results we need to send, and if
  824. 29:02we need to sort it or not. So, we are
  825. 29:04going to be taking out all of these
  826. 29:06things from our request. So, I'll say
  827. 29:08const, and here I'll take out the
  828. 29:10status, sort,
  829. 29:12page, and the limit. Okay?
  830. 29:15Below this, we are going to be using
  831. 29:17this read data over here and read the
  832. 29:19data of our file. So, I'll say const
  833. 29:20result equals read data. Then, we're
  834. 29:22going to be writing logic for each one
  835. 29:24of these, right? So, first of all, for
  836. 29:25the status, I'm going to check if status
  837. 29:28has been provided by the user.
  838. 29:30I'll say result.filter. We are taking
  839. 29:32all of our applications, and I'm saying
  840. 29:34applications.status
  841. 29:35is equals to this particular status,
  842. 29:37only then return it inside of this
  843. 29:39results right here. Okay? Then, I'm
  844. 29:41going to check if sort is equals to
  845. 29:45date. That means we want to sort it with
  846. 29:47respect to the date, right? Then,
  847. 29:48simply, we are going to be using this
  848. 29:50dot sort right here. So, I'll say
  849. 29:51result.slice and dot sort. We're going
  850. 29:54to be taking A and B over here. That is
  851. 29:56the first and the second. So, if we are
  852. 29:58going to say if A.applied date is more
  853. 30:00than B.applied date, then it will be
  854. 30:02one, otherwise minus one. So, sort is in
  855. 30:04itself a complex function. You guys can,
  856. 30:06uh, you know, search Roadside Coder
  857. 30:08sort, and you will find my explanation
  858. 30:10on YouTube. So, basically, we're sorting
  859. 30:11it in the descending order if it's one.
  860. 30:14If it's minus one, it is ascending order
  861. 30:15if I am not wrong. After this, if the
  862. 30:18page and the limit is provided, we're
  863. 30:20going to take both of them in this
  864. 30:22variable over here, right? We're going
  865. 30:24to format it into number.
  866. 30:26And after that, I'll say const start
  867. 30:29equals page num minus one into whatever
  868. 30:33the limit is. So, we're going to
  869. 30:36basically start it with respect to
  870. 30:37whatever page that we are in. If we are
  871. 30:39on one, we're going to be going from
  872. 30:40zero to whatever the limit is. Like, for
  873. 30:43example, if it's 10, so 1 to 10. If it's
  874. 30:46page number is two, so we will start
  875. 30:47from 11 to 20. Something like that,
  876. 30:49right? So, we will say result.slice
  877. 30:52start and go until whatever the limit
  878. 30:54is. Start plus limit num. And then in
  879. 30:57the end, we need to send the response,
  880. 30:58right? So, we need to write again and
  881. 31:00again response.status, whatever, right?
  882. 31:02So, the better way will be to create a
  883. 31:04helper function for this. So, for
  884. 31:06success response, I'm going to be
  885. 31:08creating send
  886. 31:09success with response, status, and data
  887. 31:14inside of it. I'll say response.status,
  888. 31:16whatever the status code is. Oops, like
  889. 31:19this. I'll say dot json. Inside of this,
  890. 31:22I'll say
  891. 31:23success to be true and the data that we
  892. 31:26want to send. Similarly, if you want to
  893. 31:28send the error, we're going to be having
  894. 31:30a function for send error as well. So,
  895. 31:31response, status, and message. I'm going
  896. 31:33to say response.status to be status and
  897. 31:36dot json.
  898. 31:37Success to be false and error to be
  899. 31:39message. Now, for this controller, I'm
  900. 31:41simply going to say send success with
  901. 31:43the response, code 200, and our result.
  902. 31:46Also, by the way, I realized this has to
  903. 31:48be app.use,
  904. 31:50not app.get. Now, up until this point,
  905. 31:52we were using
  906. 31:54a browser to hit the endpoints, right?
  907. 31:56So, slash up Oh, what is this? Internal
  908. 31:59server error. So, okay. So, actually,
  909. 32:02right here in our controllers, instead
  910. 32:04of read file, for now, we are going to
  911. 32:06be reading like using read file sync
  912. 32:08because we are using this static file
  913. 32:10over here, right? We're not making calls
  914. 32:11to any database. And for write file as
  915. 32:14well, write file sync. Now, if we use
  916. 32:16this, yes, we can see the data right
  917. 32:18here. So, as I was saying,
  918. 32:19we were using browser up until this
  919. 32:22point to check our API responses, but
  920. 32:24this is not the good practice. In the
  921. 32:26production apps or in the companies,
  922. 32:28what people generally do is they would
  923. 32:30use something like Thunder Client.
  924. 32:32Right, you can go to the extensions tab
  925. 32:34and search Thunder Client and install
  926. 32:36it. So, if you click on new request and
  927. 32:38let's paste endpoint right here and
  928. 32:41click on send. You will get the response
  929. 32:44right here and it is much more easier to
  930. 32:45test it out. So, if I let's say add
  931. 32:49status to be Okay, over here we can add
  932. 32:52applied, right? And we can say page
  933. 32:56to be one
  934. 32:57and limit to be I don't know, two. So,
  935. 33:00it will just It should give us two
  936. 33:02results now. So, if I say Yes, after
  937. 33:04sending it, we are getting two results.
  938. 33:06Awesome. So, it is much more easier to
  939. 33:08test out our APIs right here. We can
  940. 33:10also change it to post, put, delete,
  941. 33:12etc. And this is honestly the industry
  942. 33:14standard way. We can also use something
  943. 33:16like Postman as well, but I prefer this.
  944. 33:18This is right inside of our VS Code, so
  945. 33:21it's much better.
  946. 33:22So, I'll go to our applications.route.js
  947. 33:25and let's create our second endpoint for
  948. 33:28getting the single application by ID.
  949. 33:30So, over here in the
  950. 33:31applications.controller,
  951. 33:33I'll just
  952. 33:34minimize this one and here let me just
  953. 33:37copy this up actually.
  954. 33:39Let's export start get applications
  955. 33:42or get application by ID. First of all,
  956. 33:44obviously this is going to be {slash}
  957. 33:46application {slash} some number, right?
  958. 33:48So, we would need to take out that
  959. 33:50particular number that we're trying to
  960. 33:51find. Inside of it, I'm going to say
  961. 33:53const ID equals number and
  962. 33:55request.params.
  963. 33:57So, the params is what we need. .id. Let
  964. 34:00me actually take this and put it right
  965. 34:03here.
  966. 34:04And import it as well. Something like
  967. 34:07this. Now, right after this, we are
  968. 34:09going to be finding if that application
  969. 34:11even exists inside of our database. So,
  970. 34:12I'll say const application equals read
  971. 34:15data.find
  972. 34:17like each and every application, I'll
  973. 34:19say application
  974. 34:20.id if it's equals to this ID that we
  975. 34:24have written right here. Right? If it
  976. 34:26does not exist, we are going to return
  977. 34:28the send error helper function that we
  978. 34:30had created right here.
  979. 34:32Response, 404 application not found.
  980. 34:35After this, we are going to be sending
  981. 34:37the success. Okay, this should be much
  982. 34:39more straightforward. Let's go to this
  983. 34:41under ID. I'll create a new request.
  984. 34:42I'll say localhost:3000
  985. 34:45applications and slash uh let's say two
  986. 34:48over here, right? And click on send.
  987. 34:51There we go. We are getting it right
  988. 34:53here. Awesome. All right. Now next,
  989. 34:55let's create this stats route. And
  990. 34:58actually, you know what? I already told
  991. 34:59you how you can create different
  992. 35:00different get routes, right? So, I want
  993. 35:03you guys to work on this get stats
  994. 35:06route. Let me just import it right here.
  995. 35:08Something like this. And it's going to
  996. 35:10be very simple, actually. We are going
  997. 35:12to be, you know, filtering them with
  998. 35:14respect to the
  999. 35:15applied, interview, etc., etc. And you
  1000. 35:18know, you can give the output like this,
  1001. 35:22right? Like applied two, interview one,
  1002. 35:24etc. So, I guess you can use something
  1003. 35:26like reduce over here. I guess that's
  1004. 35:28enough hint for this one. Let's go on
  1005. 35:30and start working on our post route,
  1006. 35:33that is creating our applications. So,
  1007. 35:35for that, let me just add the comment.
  1008. 35:37And actually, let me just add more
  1009. 35:38comments over here, like for updating an
  1010. 35:41application, deleting an application.
  1011. 35:44So, I'll say router.post.
  1012. 35:47This will be a slash route, comma I'm
  1013. 35:49going to be naming it create
  1014. 35:50application. So, I'll go to
  1015. 35:51applications.controller right here.
  1016. 35:54Right below this, I'll say
  1017. 35:55exports.create application. And let's
  1018. 35:58just
  1019. 35:59paste it right here. Import it. Let me
  1020. 36:01also create the controllers for the
  1021. 36:04update application and the delete
  1022. 36:06application. And let me import these as
  1023. 36:09well right here. So, for update, we are
  1024. 36:11going to be saying router.put,
  1025. 36:14and it will be slash application slash
  1026. 36:16particular ID over here. And we're going
  1027. 36:17to be taking
  1028. 36:19the update application. Let's import it.
  1029. 36:21Similar to this, for deleting it as
  1030. 36:23well, we are going to be providing the
  1031. 36:24ID and we can delete it, right? So,
  1032. 36:26let's import that as well. Cool. Let's
  1033. 36:28first start working on our create
  1034. 36:30application, the post request.
  1035. 36:32And by the way, if you noticed, we have
  1036. 36:34used dot delete over here for the delete
  1037. 36:35request.
  1038. 36:37Okay. So, obviously, to create an
  1039. 36:38application, we would need to take out
  1040. 36:40like company, role, and status from the
  1041. 36:42request's body that user will send to
  1042. 36:45our API. So, I'll say
  1043. 36:47const company, role, and status. We're
  1044. 36:49going to be destructuring it from our
  1045. 36:50request.body. After this, I'll check if
  1046. 36:53company or role does not exist, I'll
  1047. 36:56return the error. So, I'll say return
  1048. 36:57send error again, the helper function
  1049. 36:59that we created, the response 400 error,
  1050. 37:02company and role are required.
  1051. 37:04Okay. After this, let's get all of the
  1052. 37:06applications using this read data
  1053. 37:08function.
  1054. 37:09And then, with respect to the last ID of
  1055. 37:11the application that we have created, we
  1056. 37:13are going to be creating the next ID.
  1057. 37:15So, I'll say const next ID equals
  1058. 37:17applications.length.
  1059. 37:19If length if it's more than Oops,
  1060. 37:21length. If it's more than zero, I'll say
  1061. 37:24Math.max,
  1062. 37:27and I'll take all of the applications,
  1063. 37:29and I'll say dot map for each and every
  1064. 37:31application ID, and I'll say plus one to
  1065. 37:33the last ID. Otherwise, we're just going
  1066. 37:35to call it one. So, that means it is the
  1067. 37:37first application. We don't have any
  1068. 37:39applications as of now. Whichever will
  1069. 37:41be the max, we will get it from over
  1070. 37:42here. And obviously, the last one will
  1071. 37:44be the max one. So, we'll take that, and
  1072. 37:46we will say plus one to it. After this,
  1073. 37:48I'll take the current date. So, I'll say
  1074. 37:50apply date equals new date. To ISO
  1075. 37:52string. Dot split from T. I'm not going
  1076. 37:55much in-depth in this. This is like
  1077. 37:56basic JavaScript concepts. And then
  1078. 37:58finally, if we are going to be creating
  1079. 37:59a new application, I'll create a new
  1080. 38:02variable over here for new application.
  1081. 38:03I'll say ID to be this next ID. Company,
  1082. 38:06role, status can be other status, the
  1083. 38:08whatever user has sent, or we can say
  1084. 38:10applied by default. And also, the
  1085. 38:12applied date. Right? Finally, I'll say
  1086. 38:14applications.
  1087. 38:16push. So, what whatever the applications
  1088. 38:19that we read. Right? So, new
  1089. 38:20application, and I'll say write data
  1090. 38:23over here. Now, this will be an
  1091. 38:24asynchronous operation. I'll say
  1092. 38:26application or applications, actually.
  1093. 38:29After this, I'll just say send success
  1094. 38:32to response 201 status and new
  1095. 38:36application. All right, how about we go
  1096. 38:38on and try this out? So, I'll just go to
  1097. 38:40the Thunder ID. So, I'll have new
  1098. 38:42request over here. Actually, uh let me
  1099. 38:44just go right here and create a separate
  1100. 38:46new request.
  1101. 38:48I'll say localhost
  1102. 38:50applications and maybe this
  1103. 38:52applications, yeah. And I'll say post
  1104. 38:54over here. And in the body, we can
  1105. 38:56provide the content. Like I'll say
  1106. 38:58company
  1107. 39:00to be, let's say, Nvidia, role
  1108. 39:03to be SWE E
  1109. 39:06and status can be interview. Let's click
  1110. 39:10on send. What's the error? Internal
  1111. 39:11server error. I guess we need to put
  1112. 39:14this in double quotes, something like
  1113. 39:16this, and click on send. Yes, now it has
  1114. 39:18been created. Let's go on and check
  1115. 39:19inside of data.json. There we go. We
  1116. 39:22have created a new entry inside of our
  1117. 39:24file DB.
  1118. 39:25Awesome. Let's go back and start working
  1119. 39:28on the update application as well. And
  1120. 39:30again, I would highly encourage you guys
  1121. 39:32to work on this yourself first and then
  1122. 39:34see my solution. So, this will be pretty
  1123. 39:36straightforward. We will take out the
  1124. 39:38param, the ID param. We will read all of
  1125. 39:41the applications and then find that
  1126. 39:43particular application with this ID.
  1127. 39:45Straightforward again. And I'm going to
  1128. 39:47check if the application does not exist,
  1129. 39:49I'm going to send the error application
  1130. 39:51not found. Otherwise, we will get the
  1131. 39:53updated data from the user. So, user
  1132. 39:56will send us company, role, and status,
  1133. 39:57whatever they want to update. And then
  1134. 39:59I'll check if company is not equals to
  1135. 40:02undefined, then we will, you know,
  1136. 40:03update the company in that particular
  1137. 40:04application. Same for role, same for a
  1138. 40:07status. And after that, I'm going to
  1139. 40:09write the data to our applications array
  1140. 40:13right here. And notice how I'm directly
  1141. 40:14writing it with this without uh
  1142. 40:16reassigning it. because since we are
  1143. 40:19making the change to application
  1144. 40:20directly, it will be making the change
  1145. 40:22to this as well. This is how, you know,
  1146. 40:23JavaScript object works.
  1147. 40:25by reference. And after this, I'll say
  1148. 40:28send success response 200 application.
  1149. 40:32Okay, let's try this one out as well.
  1150. 40:33New request. How about we go on and
  1151. 40:36update? Let's check what we need to
  1152. 40:37update. Let's just update this one only.
  1153. 40:39So, I'll take all of these three things.
  1154. 40:43And here I'm going to create a new
  1155. 40:44request for localhost. Let's say
  1156. 40:47applications will be four, and this will
  1157. 40:50be a put request. Right? And the body,
  1158. 40:53I'll actually copy it up from post.
  1159. 40:56Where is it? There it is. I'll just copy
  1160. 40:58it up. Paste it right here. Instead of
  1161. 40:59Nvidia, how about we say open AI. Click
  1162. 41:03on send.
  1163. 41:04Looks like it has been updated. Let's go
  1164. 41:06on and check. Let's adjust this one.
  1165. 41:07Yes.
  1166. 41:08It has been replaced by awesome.
  1167. 41:10Finally, let's go on and build our
  1168. 41:12delete application as well. So, inside
  1169. 41:14of this, to create a delete route in
  1170. 41:17Node.js,
  1171. 41:18we said router.delete, whatever the
  1172. 41:21endpoint is, and the delete application,
  1173. 41:23whatever the controller is, right? And
  1174. 41:24let's go on and write the logic inside
  1175. 41:26of it. Again, similar to previously, we
  1176. 41:28are going to be taking
  1177. 41:30the application. So, I'll We're going to
  1178. 41:31be hitting the ID, right? So, I'll say
  1179. 41:33const ID. I'll take it from the params.
  1180. 41:35I'll take all of the final application
  1181. 41:37and find that particular application.
  1182. 41:39Right? But, then in this case, it won't
  1183. 41:40be find. It will be actually be find
  1184. 41:43index because we're going to be deleting
  1185. 41:45it, that particular index.
  1186. 41:47After this, I'll say if index is equals
  1187. 41:51to minus one, that is it does not exist,
  1188. 41:53then I'll return the send error 404
  1189. 41:56application not found.
  1190. 41:57After this, we're going to say
  1191. 41:58applications.splice. So, splice will
  1192. 42:01help us delete something from a
  1193. 42:02particular index. So, I'm saying from
  1194. 42:05this index, delete one element. After
  1195. 42:07this, say write data with the
  1196. 42:08application, and I'll say
  1197. 42:11response.status 204. And
  1198. 42:13you know, since we don't have any
  1199. 42:14content, so we will skip the helpers and
  1200. 42:16just end the response. All right, let's
  1201. 42:18go and try deleting it as well. I'll
  1202. 42:20click on new request. I'll just go for
  1203. 42:22the same {slash} four.
  1204. 42:24And I'll say delete. Click on send. Oh.
  1205. 42:28Something's wrong. Let's check. It's
  1206. 42:29saying index is not defined. Let's see.
  1207. 42:32Right here. Oh, my bad.
  1208. 42:34I should have
  1209. 42:35said index over here. Let's try this
  1210. 42:37again. And yep, no content. Let's go
  1211. 42:40inside of our data.json and yes, it has
  1212. 42:42been removed. Awesome.
  1213. 42:44And with this, you created your very
  1214. 42:46first CRUD API. Congratulations. Now
  1215. 42:49again, if you want me to create another
  1216. 42:51video where I create authentication, you
  1217. 42:53know, add a database and all of these
  1218. 42:55things and even use TypeScript in this,
  1219. 42:57let me know in the comments down below
  1220. 42:58and I will create complete Node.js
  1221. 43:01course end-to-end with a
  1222. 43:02production-grade application. And if
  1223. 43:04you're preparing for interviews, check
  1224. 43:06the link in the description down below
  1225. 43:07for my complete interview preparation
  1226. 43:09course. I have covered a lot of
  1227. 43:10important JavaScript topics. And if
  1228. 43:12you're a front-end developer, I have
  1229. 43:13covered React JS, system design, and
  1230. 43:15much more. And you can use this code
  1231. 43:16right here to get the maximum discount.

About this transcript

This page contains the full transcript of Learn Node JS in 40 Minutes (Crash Course) - Express, CRUD APIs, etc by RoadsideCoder, generated from the public captions YouTube serves with the video. The transcript has 8,137 words across 1,231 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.