Coding

Adding More Routes to the API | Creating a REST API with Node.js

  • 00:00:02 welcome to another video in this series
  • 00:00:04 thus far in this series we had a look at
  • 00:00:07 what restful api zart
  • 00:00:09 and we started building one we only
  • 00:00:12 support one API endpoint one route one
  • 00:00:16 URL does fart and that's our route route
  • 00:00:19 and we support all HTTP words which
  • 00:00:21 cause we don't care about which one we
  • 00:00:23 got but we got a very basic restful api
  • 00:00:28 working now let's continue working on it
  • 00:00:30 in this video we'll add more routes to
  • 00:00:34 it
  • 00:00:38 so let's start improving this API and
  • 00:00:41 for that I'll start by improving the
  • 00:00:43 folder structure to make it easy to well
  • 00:00:46 to keep this project manageable I'll
  • 00:00:48 treat a new folder which will named API
  • 00:00:50 because all my API related stuff like
  • 00:00:53 the routes that the models will use when
  • 00:00:55 we work with data and so on should go in
  • 00:00:57 there so in that folder I'll treat a
  • 00:00:59 subfolder which I named routes and in
  • 00:01:02 there I will now add a product dot J's
  • 00:01:05 file which should handle my product
  • 00:01:07 related routes now how can we set up a
  • 00:01:10 route here for that I'll first of all
  • 00:01:13 again import express with require
  • 00:01:16 Express and by storing it in this
  • 00:01:18 constant Express now I will set up the
  • 00:01:22 Express router that's like a sub package
  • 00:01:25 G Express Framework ships with that
  • 00:01:27 gives us different capabilities to
  • 00:01:30 conveniently handle different routes
  • 00:01:32 reaching different endpoints with
  • 00:01:34 different HTTP words we enable it by
  • 00:01:38 simply using the Express packet we
  • 00:01:40 imported here and then calling router
  • 00:01:42 with a capital R as a function like this
  • 00:01:46 now we can start using that router to
  • 00:01:49 register different well routes now if we
  • 00:01:52 have a look at the API as we planned it
  • 00:01:55 we're at the products route here right
  • 00:01:58 now and I want you to register or at the
  • 00:02:00 products resource I should call it the
  • 00:02:02 URL is called a resource basically we're
  • 00:02:05 trying to access our products related
  • 00:02:07 resources and it will implement a get in
  • 00:02:10 a post route for that products URL then
  • 00:02:15 on the other hand means that if we send
  • 00:02:17 some other request like a patch request
  • 00:02:19 to slash products that it shouldn't
  • 00:02:21 reach this route so how do we do this on
  • 00:02:27 this router package we can add get we
  • 00:02:30 can call get get is a method that will
  • 00:02:33 handle incoming get requests the first
  • 00:02:37 argument is then the URL and that URL
  • 00:02:41 here should be slash products now to be
  • 00:02:47 precise it should be just slash because
  • 00:02:49 I want to
  • 00:02:51 load or ascend the request to this
  • 00:02:54 products file for incoming requests
  • 00:02:57 targeting anything with Slash products
  • 00:03:00 at the beginning of the URL
  • 00:03:02 so anything that reaches that foul will
  • 00:03:04 already be beyond at that point will
  • 00:03:07 already have slash products at the
  • 00:03:09 beginning so here I only need to parse
  • 00:03:12 the part after that
  • 00:03:13 now that clearly is confusing so let me
  • 00:03:15 show you how this setup works back in
  • 00:03:18 fjs i will import my product routes new
  • 00:03:23 constant product routes should require
  • 00:03:27 slash api / routes / products without
  • 00:03:32 ojs at the end now i want to make sure
  • 00:03:36 that requests are forwarded to this file
  • 00:03:40 if the URL they are targeting no matter
  • 00:03:43 which HTTP web just the URL is slash
  • 00:03:46 products for this I will work on this
  • 00:03:50 middleware where every request is
  • 00:03:52 funneled through and use a different
  • 00:03:55 format not this function with request
  • 00:03:57 response next but now the first argument
  • 00:03:59 is a filter essentially and I say slash
  • 00:04:02 product is my filter so only requests
  • 00:04:06 that start with this path EUR will then
  • 00:04:10 be handled by some handler I pass as a
  • 00:04:12 second argument and that will be my
  • 00:04:14 product routes so anything starting with
  • 00:04:18 slash products in the URL will be
  • 00:04:21 forwarded to this file and therefore in
  • 00:04:23 this file we must not use slash products
  • 00:04:27 again because that would essentially try
  • 00:04:29 to fit slash products slash products
  • 00:04:31 which we of course don't want to fit so
  • 00:04:33 we can just say slash here and this will
  • 00:04:36 essentially target URLs or requests
  • 00:04:38 aimed at slash product slash nothing in
  • 00:04:42 this case ok so I hope this is clear
  • 00:04:44 we're just using this setup so that we
  • 00:04:47 don't have to manage all the files in
  • 00:04:49 one file but can split them on a feature
  • 00:04:52 basis so this is the get method we now
  • 00:04:56 tell it which kind of sup route or sup
  • 00:05:00 URL we want to handle and the second
  • 00:05:02 argument that is a handler and that
  • 00:05:04 again is just a
  • 00:05:05 function where we have to request the
  • 00:05:06 response and this next function we
  • 00:05:08 already saw in the last video now we can
  • 00:05:13 handle get requests here and for now I
  • 00:05:16 again want to send a response where we
  • 00:05:18 set status code 200 jason and return a
  • 00:05:22 JSON object where i will set a message
  • 00:05:25 property and you could send a different
  • 00:05:27 one where I say handling requests get
  • 00:05:32 requests to slash products novel copy
  • 00:05:36 that setup and add a second route with
  • 00:05:39 the post method and this will now handle
  • 00:05:41 post requests to this path therefore
  • 00:05:45 here will output handling post requests
  • 00:05:47 to slash products and now I will simply
  • 00:05:51 say module exports router so that the
  • 00:05:54 router with the routes we configured is
  • 00:05:56 exported and can be used in other files
  • 00:05:59 like the app.js file where we import
  • 00:06:02 these routes and then we assign them
  • 00:06:05 here let's try this out let's now start
  • 00:06:09 the server again with node server j/s
  • 00:06:11 and let's go back to postman which we
  • 00:06:13 already used in the last video there
  • 00:06:17 I'll again send a post request to
  • 00:06:20 localhost 3000 and I get back an error
  • 00:06:24 now that makes sense because I was
  • 00:06:27 sending a post request to just localhost
  • 00:06:30 3000 and I got no single method in my
  • 00:06:32 code that would handle such a request we
  • 00:06:35 forward all requests to slash products
  • 00:06:38 to our products routing file here and I
  • 00:06:41 got nothing else
  • 00:06:42 so error handling is something we can
  • 00:06:44 improve because right now I just get
  • 00:06:46 some HTML text some default error page
  • 00:06:49 basically and I was talking about HTML
  • 00:06:52 not being that useful to us so we
  • 00:06:54 probably also want to return JSON data
  • 00:06:57 in error cases but let's focus on error
  • 00:06:59 handling later let's for now make this
  • 00:07:01 work by targeting localhost 3000 slash
  • 00:07:04 products now if we send this we get back
  • 00:07:08 handling post requests to slash products
  • 00:07:11 and if I do the same for it get I get
  • 00:07:15 handling get requests to products
  • 00:07:18 if I choose some other method like put
  • 00:07:22 then I get an error again because we
  • 00:07:25 only support get and post now let's add
  • 00:07:29 routes for all the other things we set
  • 00:07:32 it up on these slide or we plan on that
  • 00:07:34 slide for products that were also
  • 00:07:38 requests to the individual products so I
  • 00:07:41 want to use a router again and I want to
  • 00:07:44 be able to get information about one
  • 00:07:46 single product so I said I have a get
  • 00:07:48 request and now keep in mind slash
  • 00:07:51 products already is at the start of the
  • 00:07:54 URL but thereafter I expect to get the
  • 00:07:57 ID encoded into the URL now I need a
  • 00:08:01 variable segment for that and with
  • 00:08:04 express we set us up with a colon and
  • 00:08:07 then any name of your choice that will
  • 00:08:10 be a name by which you later can extract
  • 00:08:12 it like product ID second argument again
  • 00:08:17 is this function with request response
  • 00:08:19 and next and then in here we can again
  • 00:08:22 return a response and maybe we even take
  • 00:08:26 a more complex code here and first of
  • 00:08:29 all we extract that product ID so ID
  • 00:08:34 could be request param this is an object
  • 00:08:41 with all the parents we have and there
  • 00:08:43 we will have a Product ID param because
  • 00:08:47 that is the name which shows here and if
  • 00:08:49 you chose a different name you have to
  • 00:08:51 extract it by that name down there let's
  • 00:08:54 now check if ID it's is let's say equal
  • 00:08:58 to special in this case I could return a
  • 00:09:03 response with status code 200 and a
  • 00:09:06 Jason data object where I say message
  • 00:09:10 you discovered the special ID and
  • 00:09:14 obviously this has absolutely zero
  • 00:09:16 purpose Albert and showing that we
  • 00:09:18 correctly read T parameter in all Africa
  • 00:09:21 cases I'll return response status Jason
  • 00:09:24 and dere let's say we also want to
  • 00:09:28 return the object with a message
  • 00:09:29 property and you could mix this up
  • 00:09:32 and maybe we do and also return the ID
  • 00:09:34 property here so ID could then be the ID
  • 00:09:38 we extract and store in this constant
  • 00:09:40 and for the other requests here or for
  • 00:09:44 the other IDs you passed an ID that
  • 00:09:48 could be what we return now let's try
  • 00:09:51 this out we should be able to now send a
  • 00:09:53 request to slash products slash some ID
  • 00:09:57 and depending on what we pass and I as
  • 00:09:59 an ID we either get back this response
  • 00:10:01 or this one so back to postman let's go
  • 00:10:05 to get because we added a gat route and
  • 00:10:07 let's add some ID or one two three
  • 00:10:10 whatever you want hit send and we get an
  • 00:10:14 error because whenever you change
  • 00:10:16 something on the server you need to
  • 00:10:18 restart it so cancel it with ctrl C and
  • 00:10:21 then rerun node server J s let's try
  • 00:10:25 again let's send this and I get you
  • 00:10:27 passed an ID awesome now let's replace
  • 00:10:30 one two three with special and now we
  • 00:10:34 discover two special ID and we also get
  • 00:10:37 back the ID showing us that extracting
  • 00:10:39 it did indeed work and we will also
  • 00:10:42 replace this code with more useful code
  • 00:10:44 throughout this series so we got this
  • 00:10:47 product ID extracted now we also wanted
  • 00:10:51 to support a patch in a delete route so
  • 00:10:54 I will just copy that route here with
  • 00:10:57 this ID in the URL and I will add a
  • 00:11:01 patch method here to support patch
  • 00:11:04 requests to that and in there I simply
  • 00:11:07 want to return rest status 200 Jason and
  • 00:11:12 there we could have a message where we
  • 00:11:14 say updated product and then I will add
  • 00:11:22 another route where I basically have the
  • 00:11:25 same but with the delete method to
  • 00:11:27 support delete requests where I say
  • 00:11:29 deleted product as a side note if you're
  • 00:11:33 wondering why I never return this
  • 00:11:35 there's no need to do so there is no
  • 00:11:38 code running after this we don't make it
  • 00:11:41 into this method here because we either
  • 00:11:43 have a patch
  • 00:11:44 or a delete or a get Enzo on request you
  • 00:11:47 only would need to add return here if
  • 00:11:50 you had some other code after this
  • 00:11:53 response where you potentially again
  • 00:11:55 send a response because sending two
  • 00:11:57 responses isn't a great idea
  • 00:11:59 now with these changes in place let's
  • 00:12:02 restart the server and let's try it out
  • 00:12:05 let's send a request a patch request and
  • 00:12:09 we get updated product if we send put it
  • 00:12:13 still fails because we don't support
  • 00:12:15 this but delete on the other hand does
  • 00:12:19 work and we get back deleted product so
  • 00:12:22 this all works and this is how we add
  • 00:12:25 our routes how we add more routes and
  • 00:12:28 how we can also work with routes that
  • 00:12:30 have an ID now we also had order related
  • 00:12:35 routes so let's add these two and I'll
  • 00:12:37 add them in a new file orders dot J s
  • 00:12:40 because I want to keep my files clean
  • 00:12:43 and focused I can copy the code from
  • 00:12:46 products jazz though put it in there but
  • 00:12:49 get rid of all the routes I just want to
  • 00:12:51 have the same imports and in the end
  • 00:12:54 export to router and then for orders if
  • 00:12:57 you recall that slide we wanted to
  • 00:12:59 support get and post calls so get to
  • 00:13:03 slash orders and I will load this file
  • 00:13:06 just as I load the products file so we
  • 00:13:09 will already ensure that we parsed it
  • 00:13:12 for slash orders at the beginning so we
  • 00:13:14 don't need it here hence there we also
  • 00:13:17 have a request a response and this next
  • 00:13:21 function which we haven't seen action
  • 00:13:22 yet because we never need it to forward
  • 00:13:25 a request we wanted to send a response
  • 00:13:27 so here I will also send a request
  • 00:13:30 response excuse me 200 status code and
  • 00:13:34 let's pass an object where we have to
  • 00:13:37 message orders were fetched and of
  • 00:13:42 course for the serious people update
  • 00:13:44 this to really return a list of orders
  • 00:13:46 now let me also create another route
  • 00:13:51 post to orders
  • 00:13:53 where we can say order was created and
  • 00:13:57 maybe we want to return status code 200
  • 00:13:59 in one year which is the appropriate
  • 00:14:01 status code for saying everything was
  • 00:14:04 successful resource created hence for
  • 00:14:07 products for the post request we should
  • 00:14:10 also return 201 now that I think about
  • 00:14:12 it so make sure to update this too and
  • 00:14:15 back in orders I now also want to have
  • 00:14:19 routes for getting information about an
  • 00:14:22 individual order so I will copy this set
  • 00:14:25 up here place it here handle get
  • 00:14:28 requests to slash orders slash and now
  • 00:14:31 here comes to order ID and our dynamic
  • 00:14:35 path parameter hence the colon there
  • 00:14:37 I'll send back status code 200 and send
  • 00:14:42 back some order details possibly with
  • 00:14:45 the order our ID which we could get on
  • 00:14:49 the request parents object by accessing
  • 00:14:52 whichever name you chose here in the
  • 00:14:55 path and let's do very same for deleting
  • 00:15:01 an order there I will use the delete
  • 00:15:05 route here the delete method to handle
  • 00:15:08 the riddle eat request suitors ID and
  • 00:15:10 there I can say order delete it now
  • 00:15:13 again that's restart this server and
  • 00:15:16 let's try it out let's send a request a
  • 00:15:20 get request to slash orders and it fails
  • 00:15:24 why because creating the orders in that
  • 00:15:29 file is nice
  • 00:15:30 all these routes right but we don't
  • 00:15:33 forward requests to that file yet we do
  • 00:15:36 to products we do that in the app choice
  • 00:15:38 file with this line here we need to do
  • 00:15:41 the same for orders so I will duplicate
  • 00:15:45 this import here and name this order
  • 00:15:48 routes and I will go to slash IP I slash
  • 00:15:52 routes orders here and then I also will
  • 00:15:57 handle requests going to orders with my
  • 00:16:01 order routes so with the route setup in
  • 00:16:04 that file and now with this we can
  • 00:16:06 restart this
  • 00:16:07 server and try again sending a get
  • 00:16:09 request to slash orders and now we fetch
  • 00:16:12 them now let's try a post request which
  • 00:16:15 should also work and I see order was
  • 00:16:19 created here you also see the status
  • 00:16:22 code 201 here and now let's try order /
  • 00:16:27 some order ID can be a number can be a
  • 00:16:30 string let's try a get request order
  • 00:16:34 details and we also see the ID and let's
  • 00:16:36 try a delete request and we see order
  • 00:16:39 deleted and the ID
  • 00:16:41 so all these routes are working they're
  • 00:16:44 not doing super useful things yet but
  • 00:16:47 one thing we already got is a basic API
  • 00:16:51 not perfect not complete but we're
  • 00:16:54 getting there now let's enhance our
  • 00:16:57 setup and things like error handling and
  • 00:17:00 so on in the next video