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

description
Our RESTful API needs more Routes! Learn how to add more routes to our Node + Express project! Join the full Node.js course: https://acad.link/nodejs Check out all our other courses: https://academind.com/learn/our-courses ---------- Full Playlist: https://academind.com/learn/node-js/building-a-restful-api-with/ Source Code: https://academind.com/learn/node-js/building-a-restful-api-with/adding-more-routes-to-the-api/ ---------- • Go to https://www.academind.com and subscribe to our newsletter to stay updated and to get exclusive content & discounts • Follow @maxedapps and @academind_real on Twitter • Join our Facebook community on https://www.facebook.com/academindchannel/ See you in the videos! ---------- Academind is your source for online education in the areas of web development, frontend web development, backend web development, programming, coding and data science! No matter if you are looking for a tutorial, a course, a crash course, an introduction, an online tutorial or any related video, we try our best to offer you the content you are looking for. Our topics include Angular, React, Vue, Html, CSS, JavaScript, TypeScript, Redux, Nuxt.js, RxJs, Bootstrap, Laravel, Node.js, Progressive Web Apps (PWA), Ionic, React Native, Regular Expressions (RegEx), Stencil, Power BI, Amazon Web Services (AWS), Firebase or other topics, make sure to have a look at this channel or at academind.com to find the learning resource of your choice!

detail
{'title': 'Adding More Routes to the API | Creating a REST API with Node.js', 'heatmap': [{'end': 385.147, 'start': 344.906, 'weight': 0.803}], 'summary': "Tutorial focuses on enhancing a basic restful api by adding more routes, restructuring folder organization, creating subfolders, setting up 'products.js' file, configuring get, post, patch, and delete routes for products and orders, and utilizing middleware to filter requests.", 'chapters': [{'end': 282.828, 'segs': [{'end': 134.407, 'src': 'embed', 'start': 32.567, 'weight': 0, 'content': [{'end': 34.591, 'text': "we'll add more routes to it.", 'start': 32.567, 'duration': 2.024}, {'end': 46.389, 'text': "let's start improving this api and for that i'll start by improving the folder structure to make it easy to well.", 'start': 39.022, 'duration': 7.367}, {'end': 53.095, 'text': "to keep this project manageable, i'll create a new folder which i'll name api, because all my api related stuff,", 'start': 46.389, 'duration': 6.706}, {'end': 58.6, 'text': 'like the routes that the models will use when we work with data and so on, it should go in there.', 'start': 53.095, 'duration': 5.505}, {'end': 69.465, 'text': "so in that folder i'll create a subfolder which i name routes and in there i will now add a products.js file which should handle my product related routes.", 'start': 58.6, 'duration': 10.865}, {'end': 72.706, 'text': 'now, how can we set up a route here for that?', 'start': 69.465, 'duration': 3.241}, {'end': 83.25, 'text': "i'll first of all again import express with require express and by storing it in this constant express now i will set up the express router.", 'start': 72.706, 'duration': 10.544}, {'end': 87.631, 'text': "that's like a sub package the express framework ships with.", 'start': 83.769, 'duration': 3.862}, {'end': 97.396, 'text': 'that gives us different capabilities to conveniently handle different routes reaching different endpoints with different http words.', 'start': 87.631, 'duration': 9.765}, {'end': 105.74, 'text': 'we enable it by simply using the express package we imported here and then calling router with a capital r as a function like this.', 'start': 97.396, 'duration': 8.344}, {'end': 112.494, 'text': 'now we can start using that router to register different well routes.', 'start': 107.131, 'duration': 5.363}, {'end': 120.519, 'text': "now, if we have a look at the api as we planned it, we're at the products route here right now and i want to register,", 'start': 112.494, 'duration': 8.025}, {'end': 122.88, 'text': 'or at the products resource i should call it.', 'start': 120.519, 'duration': 2.361}, {'end': 124.781, 'text': 'the url is called a resource.', 'start': 122.88, 'duration': 1.901}, {'end': 134.407, 'text': "basically we're trying to access our products related resources and i will implement a get and a post route for that products url.", 'start': 124.781, 'duration': 9.626}], 'summary': 'Improving api by restructuring folder with new routes and implementing get and post routes for products.', 'duration': 101.84, 'max_score': 32.567, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg32567.jpg'}, {'end': 256.599, 'src': 'embed', 'start': 194.031, 'weight': 4, 'content': [{'end': 195.292, 'text': 'Now that clearly is confusing.', 'start': 194.031, 'duration': 1.261}, {'end': 197.134, 'text': 'So let me show you how this setup works.', 'start': 195.312, 'duration': 1.822}, {'end': 202.378, 'text': 'Back in app.js, I will import my product routes.', 'start': 197.754, 'duration': 4.624}, {'end': 214.107, 'text': 'New constant product routes should require slash API slash routes slash products without .js at the end.', 'start': 203.019, 'duration': 11.088}, {'end': 224.755, 'text': 'Now I want to make sure that requests are forwarded to this file if the URL they are targeting, no matter which HTTP work,', 'start': 215.148, 'duration': 9.607}, {'end': 227.217, 'text': 'just the URL is slash products.', 'start': 224.755, 'duration': 2.462}, {'end': 235.41, 'text': 'For this, I will work on this middleware where every request is funneled through and use a different format.', 'start': 228.046, 'duration': 7.364}, {'end': 240.992, 'text': 'Not this function with request response next, but now the first argument is a filter essentially.', 'start': 235.59, 'duration': 5.402}, {'end': 244.174, 'text': 'And I say, slash products is my filter.', 'start': 241.293, 'duration': 2.881}, {'end': 256.599, 'text': 'So only requests that start with this path here will then be handled by some handler i pass as a second argument, and that will be my product routes.', 'start': 244.854, 'duration': 11.745}], 'summary': 'Setting up product routes in app.js to handle requests targeting /products.', 'duration': 62.568, 'max_score': 194.031, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg194031.jpg'}], 'start': 2.885, 'title': 'Enhancing restful api', 'summary': "Focuses on enhancing a basic restful api by adding more routes, restructuring the folder organization, creating an 'api' folder and 'routes' subfolder, and setting up a 'products.js' file. it also explains the configuration of api routes, including the implementation of get and post routes for products, and the use of middleware to filter requests.", 'chapters': [{'end': 112.494, 'start': 2.885, 'title': 'Improving restful api with more routes', 'summary': "Discusses improving a basic restful api by adding more routes and improving the folder structure to make the project manageable, with a focus on creating a new 'api' folder and a 'routes' subfolder, and setting up a new 'products.js' file to handle product related routes.", 'duration': 109.609, 'highlights': ["Improving the folder structure by creating a new 'api' folder and a 'routes' subfolder to keep the project manageable.", "Adding a 'products.js' file in the 'routes' subfolder to handle product related routes.", 'Setting up the express router to conveniently handle different routes reaching different endpoints with different HTTP verbs.']}, {'end': 282.828, 'start': 112.494, 'title': 'Api route configuration', 'summary': 'Explains the configuration of api routes, including the implementation of get and post routes for products, and the use of middleware to filter requests targeting the /products url, ensuring that only requests starting with this path are handled by the product routes.', 'duration': 170.334, 'highlights': ['The chapter explains the implementation of get and post routes for the products URL, ensuring efficient access to the products related resources.', 'The use of middleware to filter requests targeting the /products URL ensures that only requests starting with this path are handled by the product routes, improving request handling efficiency.', 'The explanation of how the setup works, including the forwarding of requests to the product routes file if the URL targets /products, demonstrates a practical understanding of route configuration and request handling.']}], 'duration': 279.943, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg2885.jpg', 'highlights': ['Setting up the express router to conveniently handle different routes reaching different endpoints with different HTTP verbs.', "Adding a 'products.js' file in the 'routes' subfolder to handle product related routes.", "Improving the folder structure by creating a new 'api' folder and a 'routes' subfolder to keep the project manageable.", 'The chapter explains the implementation of get and post routes for the products URL, ensuring efficient access to the products related resources.', 'The use of middleware to filter requests targeting the /products URL ensures that only requests starting with this path are handled by the product routes, improving request handling efficiency.', 'The explanation of how the setup works, including the forwarding of requests to the product routes file if the URL targets /products, demonstrates a practical understanding of route configuration and request handling.']}, {'end': 649.955, 'segs': [{'end': 331.975, 'src': 'embed', 'start': 283.549, 'weight': 2, 'content': [{'end': 285.37, 'text': 'OK, so I hope this is clear.', 'start': 283.549, 'duration': 1.821}, {'end': 293.294, 'text': "We're just using this setup so that we don't have to manage all the files in one file, but can split them on a feature basis.", 'start': 285.69, 'duration': 7.604}, {'end': 296.177, 'text': 'so this is the get method.', 'start': 294.275, 'duration': 1.902}, {'end': 301.743, 'text': 'we now tell it which kind of sub route or sub url we want to handle.', 'start': 296.177, 'duration': 5.566}, {'end': 304.045, 'text': 'and the second argument then is a handler,', 'start': 301.743, 'duration': 2.302}, {'end': 310.732, 'text': 'and that again is just a function where we have to request response and this next function we already saw in the last video.', 'start': 304.045, 'duration': 6.687}, {'end': 331.975, 'text': 'now we can handle get requests here and for now i again want to send a response where we set status code 200 json and return a json object where i will set a message property and you could send a different one where i say handling requests,', 'start': 312.296, 'duration': 19.679}], 'summary': 'Using setup to manage files, handling get requests, sending response with status code 200 and json object.', 'duration': 48.426, 'max_score': 283.549, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg283549.jpg'}, {'end': 385.147, 'src': 'heatmap', 'start': 335.959, 'weight': 0, 'content': [{'end': 340.783, 'text': 'Now I will copy that setup and add a second route with the post method.', 'start': 335.959, 'duration': 4.824}, {'end': 344.386, 'text': 'And this will now handle post requests to this path.', 'start': 341.063, 'duration': 3.323}, {'end': 348.669, 'text': 'Therefore here will output handling post requests to slash products.', 'start': 344.906, 'duration': 3.763}, {'end': 360.458, 'text': 'And now I will simply say module exports, router, so that the router, with the routes we configured, is exported and can be used in other files,', 'start': 350.19, 'duration': 10.268}, {'end': 363.881, 'text': 'like the app.js file where we import these routes.', 'start': 360.458, 'duration': 3.423}, {'end': 367.264, 'text': 'and then we assign them here.', 'start': 364.421, 'duration': 2.843}, {'end': 368.305, 'text': "let's try this out.", 'start': 367.264, 'duration': 1.041}, {'end': 376.572, 'text': "let's now start the server again with node server js and let's go back to postman, which we already used in the last video.", 'start': 368.305, 'duration': 8.267}, {'end': 385.147, 'text': "there i'll again send a post request to localhost 3000 and i get back an error.", 'start': 376.572, 'duration': 8.575}], 'summary': 'Setting up a second route for post requests, handling requests to /products, and exporting the router for use in other files.', 'duration': 27.922, 'max_score': 335.959, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg335959.jpg'}, {'end': 561.712, 'src': 'embed', 'start': 529.235, 'weight': 3, 'content': [{'end': 532.916, 'text': 'And if you chose a different name, you have to extract it by that name down there.', 'start': 529.235, 'duration': 3.681}, {'end': 540.378, 'text': "Let's now check if ID is, let's say, equal to special.", 'start': 533.856, 'duration': 6.522}, {'end': 554.247, 'text': 'In this case, I could return a response with status code 200 and a JSON data object where I say message, you discovered the special ID.', 'start': 541.42, 'duration': 12.827}, {'end': 560.031, 'text': 'And obviously this has absolutely zero purpose other than showing that we correctly read the parameter.', 'start': 554.648, 'duration': 5.383}, {'end': 561.712, 'text': 'in all africa.', 'start': 560.891, 'duration': 0.821}], 'summary': "Check if id is equal to special, return status code 200 with message 'discovered the special id'", 'duration': 32.477, 'max_score': 529.235, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg529235.jpg'}], 'start': 283.549, 'title': 'Node.js and express route handling', 'summary': 'Covers utilizing node.js router for get and post requests, emphasizing file splitting and route export. it also delves into error handling enhancement and setting up routes in express, including post and get requests for products, and extracting/responding to product id in json format.', 'chapters': [{'end': 363.881, 'start': 283.549, 'title': 'Node.js route handling', 'summary': 'Covers using the node.js router to handle get and post requests, with an emphasis on splitting files on a feature basis and exporting the configured routes for use in other files.', 'duration': 80.332, 'highlights': ['The chapter emphasizes the use of the Node.js router to handle get and post requests, with a focus on splitting files on a feature basis and exporting the configured routes for use in other files.', 'The setup allows for the management of files on a feature basis, and the ability to split them rather than managing all in one file.', 'The post method is used to handle post requests to a specific path, with the output indicating the handling of post requests to slash products.', 'The get method is utilized to handle get requests, with the response setting a status code of 200 and returning a JSON object with a message property, demonstrating the handling of get requests to slash products.']}, {'end': 649.955, 'start': 364.421, 'title': 'Error handling and route setup in express', 'summary': 'Covers error handling improvement and setting up routes for different requests in express, including handling post and get requests for products, and extracting and responding to the product id in json format, with an example of special id.', 'duration': 285.534, 'highlights': ['Setting up routes for handling different requests such as get and post for products in Express The chapter discusses setting up routes for handling different requests, such as get and post, for products in Express, with an emphasis on error handling and improvement.', 'Extracting and responding to the product ID in JSON format, with an example of special ID The chapter explains how to extract and respond to the product ID in JSON format, including an example of a special ID, showcasing the correct reading of the parameter.', 'Improvement in error handling by returning JSON data in error cases The chapter mentions the improvement in error handling by discussing the return of JSON data in error cases, emphasizing the need to return JSON data instead of default HTML text.']}], 'duration': 366.406, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg283549.jpg', 'highlights': ['The chapter emphasizes the use of the Node.js router to handle get and post requests, with a focus on splitting files on a feature basis and exporting the configured routes for use in other files.', 'Setting up routes for handling different requests such as get and post for products in Express, with an emphasis on error handling and improvement.', 'The setup allows for the management of files on a feature basis, and the ability to split them rather than managing all in one file.', 'Extracting and responding to the product ID in JSON format, with an example of special ID.', 'The post method is used to handle post requests to a specific path, with the output indicating the handling of post requests to slash products.']}, {'end': 1021.816, 'segs': [{'end': 678.303, 'src': 'embed', 'start': 650.475, 'weight': 2, 'content': [{'end': 653.477, 'text': 'Now we also wanted to support a patch and a delete route.', 'start': 650.475, 'duration': 3.002}, {'end': 660.201, 'text': 'So I will just copy that route here with this ID in the URL.', 'start': 654.377, 'duration': 5.824}, {'end': 665.644, 'text': 'And I will add a patch method here to support patch requests to that.', 'start': 661.002, 'duration': 4.642}, {'end': 672.569, 'text': 'And in there, I simply want to return res status 200 JSON.', 'start': 666.565, 'duration': 6.004}, {'end': 678.303, 'text': 'And there we could have a message where we say, Updated product.', 'start': 673.129, 'duration': 5.174}], 'summary': 'Added support for patch and delete routes, with a 200 status and message.', 'duration': 27.828, 'max_score': 650.475, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg650475.jpg'}, {'end': 732.074, 'src': 'embed', 'start': 707.646, 'weight': 3, 'content': [{'end': 716.89, 'text': 'You only would need to add return here if you had some other code after this response, where you potentially again send a response,', 'start': 707.646, 'duration': 9.244}, {'end': 720.231, 'text': "because sending two responses isn't a great idea.", 'start': 716.89, 'duration': 3.341}, {'end': 726.112, 'text': "now, with these changes in place, let's restart the server and let's try it out.", 'start': 720.231, 'duration': 5.881}, {'end': 732.074, 'text': "let's send a request, a patch request, and we get updated product.", 'start': 726.112, 'duration': 5.962}], 'summary': "Adding 'return' is needed for potential code after response; testing with patch request for updated product.", 'duration': 24.428, 'max_score': 707.646, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg707646.jpg'}, {'end': 864.149, 'src': 'embed', 'start': 840.386, 'weight': 4, 'content': [{'end': 847.307, 'text': 'which is the appropriate status code for saying everything was successful, resource created.', 'start': 840.386, 'duration': 6.921}, {'end': 849.068, 'text': 'hence for products.', 'start': 847.307, 'duration': 1.761}, {'end': 853.609, 'text': 'for the post request we should also return 201 now that i think about it.', 'start': 849.068, 'duration': 4.541}, {'end': 854.889, 'text': 'so make sure to update this too.', 'start': 853.609, 'duration': 1.28}, {'end': 857.925, 'text': 'and back in orders.', 'start': 856.164, 'duration': 1.761}, {'end': 864.149, 'text': 'i now also want to have routes for getting information about an individual order.', 'start': 857.925, 'duration': 6.224}], 'summary': 'Update status code to 201 for successful resource creation in post requests, and add routes for individual order information.', 'duration': 23.763, 'max_score': 840.386, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg840386.jpg'}, {'end': 931.699, 'src': 'embed', 'start': 897.854, 'weight': 6, 'content': [{'end': 901.835, 'text': "And let's do the very same for deleting an order.", 'start': 897.854, 'duration': 3.981}, {'end': 910.298, 'text': 'There I will use the delete route here, the delete method to handle the delete request to this ID.', 'start': 902.516, 'duration': 7.782}, {'end': 912.799, 'text': 'And there I can say order deleted.', 'start': 910.679, 'duration': 2.12}, {'end': 917.961, 'text': "Now again, let's restart this server and let's try it out.", 'start': 913.76, 'duration': 4.201}, {'end': 925.124, 'text': "Let's send a request, a get request to slash orders and it fails.", 'start': 918.661, 'duration': 6.463}, {'end': 931.699, 'text': 'Why? Because creating the orders in that file is nice, all these routes, great.', 'start': 925.504, 'duration': 6.195}], 'summary': 'Using the delete route and method to handle an order deletion request and restarting the server to test the functionality.', 'duration': 33.845, 'max_score': 897.854, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg897854.jpg'}, {'end': 1021.816, 'src': 'embed', 'start': 991.865, 'weight': 0, 'content': [{'end': 995.949, 'text': "Let's try a get request, order details and we also see the ID.", 'start': 991.865, 'duration': 4.084}, {'end': 1000.892, 'text': "And let's try a delete request and we see order deleted and the ID.", 'start': 996.689, 'duration': 4.203}, {'end': 1004.216, 'text': 'so all these routes are working.', 'start': 1001.813, 'duration': 2.403}, {'end': 1015.949, 'text': "they're not doing super useful things yet, but one thing we already got is a basic api not perfect, not complete, but we're getting there.", 'start': 1004.216, 'duration': 11.733}, {'end': 1021.816, 'text': "now let's enhance our setup and things like error handling and so on in the next video.", 'start': 1015.949, 'duration': 5.867}], 'summary': 'Tested get and delete requests for order details, ids, and routes. basic api in progress, with plans to enhance setup and error handling in the next video.', 'duration': 29.951, 'max_score': 991.865, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg991865.jpg'}], 'start': 650.475, 'title': 'Setting up routes and handling requests', 'summary': "Discusses adding patch and delete routes, and configuring routes for products and orders, resulting in a basic api setup with a demonstration of a patch request resulting in the message 'updated product'.", 'chapters': [{'end': 732.074, 'start': 650.475, 'title': 'Supporting patch and delete routes', 'summary': "Discusses adding patch and delete routes to support requests and explains the need for not returning a response in certain scenarios, with a demonstration of a patch request resulting in the message 'updated product'.", 'duration': 81.599, 'highlights': ['The chapter discusses adding patch and delete routes to support requests.', 'The need for not returning a response in certain scenarios is explained.', "A demonstration of a patch request resulting in the message 'Updated product' is shown."]}, {'end': 1021.816, 'start': 732.074, 'title': 'Configuring routes for products and orders', 'summary': 'Covers configuring routes for products and orders, including adding routes, handling get and post calls, and setting up route forwarding, resulting in a basic api setup.', 'duration': 289.742, 'highlights': ['The chapter covers configuring routes for products and orders, including adding routes, handling get and post calls, and setting up route forwarding, resulting in a basic API setup.', 'The process involves setting up routes for products and orders, adding routes, handling get and post calls, and implementing route forwarding to establish a basic API setup.', 'The chapter also emphasizes the importance of keeping files clean and focused, as well as the need to update status codes for post requests to ensure the appropriate response.', 'The chapter discusses the steps to forward requests to the orders file, enabling the functionality for sending get, post, and delete requests to interact with the orders routes.', 'The setup involves creating routes for products and orders, handling get and post calls, and ensuring the proper forwarding of requests to establish a basic API setup.']}], 'duration': 371.341, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/FV1Ugv1Temg/pics/FV1Ugv1Temg650475.jpg', 'highlights': ['The chapter covers configuring routes for products and orders, including adding routes, handling get and post calls, and setting up route forwarding, resulting in a basic API setup.', 'The setup involves creating routes for products and orders, handling get and post calls, and ensuring the proper forwarding of requests to establish a basic API setup.', 'The chapter discusses adding patch and delete routes to support requests.', 'The need for not returning a response in certain scenarios is explained.', 'The chapter also emphasizes the importance of keeping files clean and focused, as well as the need to update status codes for post requests to ensure the appropriate response.', 'The process involves setting up routes for products and orders, adding routes, handling get and post calls, and implementing route forwarding to establish a basic API setup.', 'The chapter discusses the steps to forward requests to the orders file, enabling the functionality for sending get, post, and delete requests to interact with the orders routes.', "A demonstration of a patch request resulting in the message 'Updated product' is shown."]}], 'highlights': ['The use of middleware to filter requests targeting the /products URL ensures that only requests starting with this path are handled by the product routes, improving request handling efficiency.', 'The explanation of how the setup works, including the forwarding of requests to the product routes file if the URL targets /products, demonstrates a practical understanding of route configuration and request handling.', 'The chapter emphasizes the use of the Node.js router to handle get and post requests, with a focus on splitting files on a feature basis and exporting the configured routes for use in other files.', 'The setup allows for the management of files on a feature basis, and the ability to split them rather than managing all in one file.', 'The chapter covers configuring routes for products and orders, including adding routes, handling get and post calls, and setting up route forwarding, resulting in a basic API setup.', 'The need for not returning a response in certain scenarios is explained.', 'The chapter also emphasizes the importance of keeping files clean and focused, as well as the need to update status codes for post requests to ensure the appropriate response.']}