title
Adding Controllers | Creating a REST API with Node.js
description
Thus far, all our logic resides in the route files. Let's split it up and add some controllers to the Node RESTful API!
----------
Learn Node.js in our comprehensive 30h+ course: https://acad.link/nodejs
Source Code: https://github.com/academind/node-restful-api-tutorial/tree/13-controllers
----------
• You can follow Max on Twitter (@maxedapps).
• You can also find us on Facebook.(https://www.facebook.com/academindchannel/)
• Or visit our Website (https://www.academind.com) and subscribe to our newsletter!
See you in the videos!
detail
{'title': 'Adding Controllers | Creating a REST API with Node.js', 'heatmap': [{'end': 186.09, 'start': 140.416, 'weight': 0.786}], 'summary': 'Demonstrates enhancing node.js route files for a restful service with added authentication and implementing an mvc approach in api controllers, optimizing route files for a more efficient application structure, and implementing user sign-up and user controllers with node and express, emphasizing the importance of adding middleware for user authentication and authorization.', 'chapters': [{'end': 231.113, 'segs': [{'end': 49.237, 'src': 'embed', 'start': 3.328, 'weight': 0, 'content': [{'end': 7.029, 'text': 'Welcome back to this series where we build a RESTful service with Node.js.', 'start': 3.328, 'duration': 3.701}, {'end': 13.931, 'text': "We added a lot of functionality, we added authentication and I'd call our service finished right now.", 'start': 7.629, 'duration': 6.302}, {'end': 22.673, 'text': 'You can always tweak it, you can always improve it and definitely share any tweaks, additions or alternatives you created in the comment section.', 'start': 14.311, 'duration': 8.362}, {'end': 28.254, 'text': 'In this video, I want to have a look at one improvement that certainly makes sense.', 'start': 23.213, 'duration': 5.041}, {'end': 32.034, 'text': "So let's dive right into it and see which improvement that is.", 'start': 28.834, 'duration': 3.2}, {'end': 41.945, 'text': 'As I mentioned, you can do a lot of things, but one thing I want to bring your attention to are our route files.', 'start': 35.996, 'duration': 5.949}, {'end': 45.912, 'text': "If we have a look at them, you see they're relatively big.", 'start': 42.586, 'duration': 3.326}, {'end': 48.155, 'text': 'Now, obviously not super big.', 'start': 46.072, 'duration': 2.083}, {'end': 49.237, 'text': "Everything's fine.", 'start': 48.516, 'duration': 0.721}], 'summary': 'A restful service with node.js has been completed, with added authentication and potential for further improvements discussed.', 'duration': 45.909, 'max_score': 3.328, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ03328.jpg'}, {'end': 121.264, 'src': 'embed', 'start': 77.538, 'weight': 2, 'content': [{'end': 82.461, 'text': "So therefore, I will add a new folder in the API folder and I'll name it controllers.", 'start': 77.538, 'duration': 4.923}, {'end': 87.704, 'text': 'And therefore we will get closer to an MVC approach though the V.', 'start': 83.142, 'duration': 4.562}, {'end': 90.546, 'text': "the view is of course missing because it's a RESTful service.", 'start': 87.704, 'duration': 2.842}, {'end': 91.527, 'text': "we don't render a view.", 'start': 90.546, 'duration': 0.981}, {'end': 94.889, 'text': 'But at least on the back end, we have our models.', 'start': 92.107, 'duration': 2.782}, {'end': 96.71, 'text': 'We created these with Mongoose.', 'start': 95.309, 'duration': 1.401}, {'end': 101.092, 'text': 'And then we have our controllers where the actual logic should live.', 'start': 97.35, 'duration': 3.742}, {'end': 104.754, 'text': 'And that will allow us to create cleaner route files.', 'start': 101.953, 'duration': 2.801}, {'end': 108.897, 'text': 'So now I could create a orders.js file here too.', 'start': 105.415, 'duration': 3.482}, {'end': 110.818, 'text': 'And the idea is simple.', 'start': 109.637, 'duration': 1.181}, {'end': 121.264, 'text': 'I want to export a couple of functions here in the ordered JS files that in the end just are the same functions as I pass to my router here.', 'start': 111.378, 'duration': 9.886}], 'summary': "Adding a new 'controllers' folder to achieve mvc approach with models created using mongoose.", 'duration': 43.726, 'max_score': 77.538, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ077538.jpg'}, {'end': 186.09, 'src': 'heatmap', 'start': 140.416, 'weight': 0.786, 'content': [{'end': 147.561, 'text': "now, to export it i'll add exports and then a name of which under which i want to export this.", 'start': 140.416, 'duration': 7.145}, {'end': 156.667, 'text': "and here i'll name it order, get orders, get all, maybe, and assign this function as a value.", 'start': 147.561, 'duration': 9.106}, {'end': 159.049, 'text': "and that's actually almost all.", 'start': 156.667, 'duration': 2.382}, {'end': 163.632, 'text': "now there's one thing, of course i need to get access to the order here.", 'start': 159.049, 'duration': 4.583}, {'end': 173.86, 'text': 'so i will take the order import from mongoose and copy that and put it into my orders controller here like this,', 'start': 163.632, 'duration': 10.228}, {'end': 176.321, 'text': 'so that i can still use my order here.', 'start': 173.86, 'duration': 2.461}, {'end': 186.09, 'text': 'With that added here I can go back to my route, the orders.js route, and import my controller.', 'start': 177.342, 'duration': 8.748}], 'summary': "Exported 'get orders' and 'get all' functions under 'order' name from mongoose for access in orders controller.", 'duration': 45.674, 'max_score': 140.416, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ0140416.jpg'}], 'start': 3.328, 'title': 'Node.js route files and api controllers', 'summary': 'Covers improving route files in node.js to complete a restful service with added authentication, emphasizing clear and concise file organization. it also discusses implementing an mvc approach in api controllers by creating a new controllers folder, using mongoose to create models, and exporting functions in the orders.js file for improved logic.', 'chapters': [{'end': 76.918, 'start': 3.328, 'title': 'Improving route files in node.js', 'summary': 'Covers the completion of a restful service with node.js, with added functionality including authentication. the focus is on improving route files, emphasizing the need for clear and concise file organization to distribute requests efficiently.', 'duration': 73.59, 'highlights': ['The focus is on improving route files, emphasizing the need for clear and concise file organization to distribute requests efficiently.', 'Completion of a RESTful service with Node.js, including added functionality such as authentication.', 'Emphasizing the importance of organizing route files to efficiently distribute requests.']}, {'end': 231.113, 'start': 77.538, 'title': 'Implementing mvc approach in api controllers', 'summary': 'Discusses the implementation of an mvc approach by creating a new controllers folder in the api, using mongoose to create models, and exporting functions in the orders.js file for cleaner route files and improved logic.', 'duration': 153.575, 'highlights': ["Creating a new controllers folder in the API to implement an MVC approach The chapter discusses adding a new folder named 'controllers' in the API to approach an MVC structure, which enhances the organization and separation of concerns in the codebase.", 'Using Mongoose to create models for the back end The use of Mongoose for creating models in the back end is highlighted, indicating the implementation of data schema and models for the application.', 'Exporting functions in the orders.js file for cleaner route files and improved logic The process of exporting functions in the orders.js file is discussed, aiming to create cleaner route files and improve the logic of the application, leading to enhanced maintainability and readability of the codebase.']}], 'duration': 227.785, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ03328.jpg', 'highlights': ['Completion of a RESTful service with Node.js, including added functionality such as authentication.', 'Emphasizing the importance of organizing route files to efficiently distribute requests.', 'Creating a new controllers folder in the API to implement an MVC approach.', 'Using Mongoose to create models for the back end.', 'Exporting functions in the orders.js file for cleaner route files and improved logic.']}, {'end': 891.236, 'segs': [{'end': 253.707, 'src': 'embed', 'start': 231.153, 'weight': 0, 'content': [{'end': 239.46, 'text': 'So let me save this and let me also make sure that check off here is of course incorrect in the orders controller.', 'start': 231.153, 'duration': 8.307}, {'end': 241.581, 'text': 'That should stay in the route file.', 'start': 239.94, 'duration': 1.641}, {'end': 245.765, 'text': "This still is my extra check I'm executing here in the middleware.", 'start': 241.942, 'duration': 3.823}, {'end': 246.845, 'text': 'Now with that.', 'start': 246.445, 'duration': 0.4}, {'end': 253.707, 'text': 'if I save this, it should work and now if I send a request to orders again, I get off failed.', 'start': 246.845, 'duration': 6.862}], 'summary': 'Moving check from controller to middleware resulted in failed request to orders.', 'duration': 22.554, 'max_score': 231.153, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ0231153.jpg'}, {'end': 305.698, 'src': 'embed', 'start': 280.364, 'weight': 2, 'content': [{'end': 285.57, 'text': 'You could, of course, also change your controller to also kind of expose that, but I guess..', 'start': 280.364, 'duration': 5.206}, {'end': 290.152, 'text': 'adding the middleware directly here makes sense, so you can easily see okay,', 'start': 286.491, 'duration': 3.661}, {'end': 298.795, 'text': 'any request reaching that route goes through that middleware and then ultimately is handled by this function here in my orders controller,', 'start': 290.152, 'duration': 8.643}, {'end': 301.256, 'text': 'and this is how we well can set this up.', 'start': 298.795, 'duration': 2.461}, {'end': 305.698, 'text': 'and now we got a very clean route file, at least once we did this for all the routes.', 'start': 301.256, 'duration': 4.442}], 'summary': 'Adding middleware to route for easy request handling.', 'duration': 25.334, 'max_score': 280.364, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ0280364.jpg'}, {'end': 863.382, 'src': 'embed', 'start': 831.121, 'weight': 1, 'content': [{'end': 835.463, 'text': 'And now we got a very clean and lean products routes file too.', 'start': 831.121, 'duration': 4.342}, {'end': 840.627, 'text': 'And as I said you could also outsource all the Malter related code here if you wanted to.', 'start': 835.944, 'duration': 4.683}, {'end': 850.251, 'text': "Now finally, let's create a new user.js file in the controllers folder where we manage all the logic for our user.js routes.", 'start': 841.527, 'duration': 8.724}, {'end': 863.382, 'text': "So I'll copy or cut the code here for creating a new user and export it in the user.js file here user sign up.", 'start': 851.072, 'duration': 12.31}], 'summary': 'Creating clean products routes, outsourcing malter code, and managing user logic in new user.js file.', 'duration': 32.261, 'max_score': 831.121, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ0831121.jpg'}], 'start': 231.153, 'title': 'Middleware check and route optimization', 'summary': 'Discusses implementing middleware checks in the route file, addressing incorrect placement in the orders controller, and optimizing route files for products, orders, and users, resulting in a more efficient and organized application structure.', 'chapters': [{'end': 353.454, 'start': 231.153, 'title': 'Middleware check in route file', 'summary': 'Discusses implementing middleware checks in the route file, addressing the incorrect placement in the orders controller and successfully setting up the middleware to handle requests, ultimately leading to a clean route file and correct functionality.', 'duration': 122.301, 'highlights': ['Implemented middleware checks in the route file instead of the orders controller, ensuring a clean route file and correct functionality. Middleware checks moved from the orders controller to the route file.', 'Successfully set up the middleware to handle requests and ensure correct functionality. Middleware set up to handle requests, resulting in correct functionality.', 'Discussed the importance of adding middleware directly in the controller for easy request routing and handling. Importance of adding middleware directly in the controller for easy request routing and handling.']}, {'end': 891.236, 'start': 354.294, 'title': 'Postman testing and route optimization', 'summary': 'Details the process of testing endpoints in postman, resolving errors with mongoose, and optimizing route files for products, orders, and users, resulting in a more efficient and organized application structure.', 'duration': 536.942, 'highlights': ['Resolved errors with Mongoose by adding necessary imports and dependencies, resulting in successful storage of orders. By adding the required imports and dependencies for Mongoose, the speaker was able to successfully store orders in the application.', 'Optimized route files for products, orders, and users, resulting in a cleaner and more organized structure. The speaker optimized the route files for products, orders, and users, resulting in a cleaner and more organized structure, making it easier to understand the flow of incoming requests.', 'Tested and verified endpoint functionalities in Postman, including creating, retrieving, updating, and deleting products, orders, and users. The speaker tested and verified endpoint functionalities in Postman, including creating, retrieving, updating, and deleting products, orders, and users, ensuring the proper functioning of the application.']}], 'duration': 660.083, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ0231153.jpg', 'highlights': ['Implemented middleware checks in the route file instead of the orders controller, ensuring a clean route file and correct functionality.', 'Optimized route files for products, orders, and users, resulting in a cleaner and more organized structure.', 'Successfully set up the middleware to handle requests and ensure correct functionality.']}, {'end': 1187.527, 'segs': [{'end': 943.095, 'src': 'embed', 'start': 891.597, 'weight': 3, 'content': [{'end': 892.879, 'text': "And now let's go to the controller's folder.", 'start': 891.597, 'duration': 1.282}, {'end': 902.534, 'text': 'and import the user file and then here for signing up, i can use my user controller and use the user sign up method,', 'start': 894.551, 'duration': 7.983}, {'end': 905.495, 'text': 'point to it the method i just created.', 'start': 902.534, 'duration': 2.961}, {'end': 913.839, 'text': "and with that, if we test this and we try to create a new user, so for that, don't forget we need to pass an email and a password field.", 'start': 905.495, 'duration': 8.344}, {'end': 924.904, 'text': 'so if we try this out and i turn this into a post request to slash user slash sign up can remove the content, add the authorization header,', 'start': 913.839, 'duration': 11.065}, {'end': 930.187, 'text': 'set content to application JSON, though to a raw data here which is JSON,', 'start': 924.904, 'duration': 5.283}, {'end': 943.095, 'text': 'and then simply pass on email which could be test55 at test.com and a password whoops, like this password, which could be tester and now hit send.', 'start': 930.187, 'duration': 12.908}], 'summary': 'Using the user controller, a user signed up with email test55@test.com and password tester.', 'duration': 51.498, 'max_score': 891.597, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ0891597.jpg'}, {'end': 1133.129, 'src': 'embed', 'start': 1094.423, 'weight': 1, 'content': [{'end': 1103.711, 'text': 'you would have to add some kind of authorization where you manage a different table in Mongoose where you assign roles or permissions to your users.', 'start': 1094.423, 'duration': 9.288}, {'end': 1111.378, 'text': 'That is something you can simply add to this application, but at least we got this basic protection against unauthenticated users.', 'start': 1104.152, 'duration': 7.226}, {'end': 1119.303, 'text': 'The core thing of that video, however, was that we create these controllers, and we did, and now we have very lean routing files.', 'start': 1112.119, 'duration': 7.184}, {'end': 1122.544, 'text': 'And of course we can now clean these routing files up.', 'start': 1119.843, 'duration': 2.701}, {'end': 1127.006, 'text': 'remove user JWT bcrypt mongoose from the user JS file.', 'start': 1122.544, 'duration': 4.462}, {'end': 1131.168, 'text': 'here only leave the controller and the middleware and of course Express and router.', 'start': 1127.006, 'duration': 4.162}, {'end': 1133.129, 'text': "And for products, it's the same.", 'start': 1131.689, 'duration': 1.44}], 'summary': 'Implemented basic user authentication and lean routing for improved application security and efficiency.', 'duration': 38.706, 'max_score': 1094.423, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ01094423.jpg'}, {'end': 1177.034, 'src': 'embed', 'start': 1152.608, 'weight': 0, 'content': [{'end': 1159.149, 'text': "We're using controllers to manage our logic there and to only manage routing and the route files, which I guess makes sense.", 'start': 1152.608, 'duration': 6.541}, {'end': 1160.95, 'text': "And now it's up to you.", 'start': 1160.03, 'duration': 0.92}, {'end': 1162.71, 'text': 'You can enhance this application.', 'start': 1161.03, 'duration': 1.68}, {'end': 1165.451, 'text': 'You can try to add authorization.', 'start': 1162.95, 'duration': 2.501}, {'end': 1168.752, 'text': 'So try to manage different roles for different users.', 'start': 1165.551, 'duration': 3.201}, {'end': 1172.653, 'text': 'You can, of course, add other routes, other functionalities.', 'start': 1169.252, 'duration': 3.401}, {'end': 1177.034, 'text': 'You can exchange Mongoose with a MySQL database if you want.', 'start': 1173.213, 'duration': 3.821}], 'summary': 'Using controllers to manage logic, add authorization, manage different roles, and exchange mongoose with mysql.', 'duration': 24.426, 'max_score': 1152.608, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ01152608.jpg'}], 'start': 891.597, 'title': 'Implementing user sign-up and node/express restful service', 'summary': "Covers implementing user sign-up in the controller by making a post request to '/user/sign-up' with email and password fields, and also demonstrates creating user controllers for user creation, login, and deletion using node and express, emphasizing the importance of adding middleware for user authentication and authorization.", 'chapters': [{'end': 943.095, 'start': 891.597, 'title': 'Implementing user sign-up in controller', 'summary': "Covers implementing user sign-up in the controller by importing the user file and utilizing the user controller's sign-up method, while making a post request to '/user/sign-up' with email and password fields to create a new user.", 'duration': 51.498, 'highlights': ["Import the user file and use the user controller's sign-up method to create a new user with email and password fields.", "Make a post request to '/user/sign-up' with the provided email and password fields to test the user sign-up functionality."]}, {'end': 1187.527, 'start': 945.276, 'title': 'Node and express restful service', 'summary': 'Demonstrates the process of creating user controllers for user creation, login, and deletion, and emphasizes the importance of adding middleware for user authentication and authorization, resulting in leaner routing files and providing basic protection against unauthenticated users.', 'duration': 242.251, 'highlights': ['The process of creating user controllers for user creation, login, and deletion resulted in leaner routing files.', 'Adding middleware for user authentication and authorization provided basic protection against unauthenticated users.', 'The demonstration emphasized the importance of managing different roles for different users and the possibility to add other routes and functionalities.', 'The video concluded by encouraging viewers to enhance the application and explore other possibilities such as exchanging Mongoose with a MySQL database.']}], 'duration': 295.93, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ucuNgSOFDZ0/pics/ucuNgSOFDZ0891597.jpg', 'highlights': ['Demonstration emphasizes importance of managing different roles for different users and possibility to add other routes and functionalities.', 'Adding middleware for user authentication and authorization provided basic protection against unauthenticated users.', 'The process of creating user controllers for user creation, login, and deletion resulted in leaner routing files.', "Import the user file and use the user controller's sign-up method to create a new user with email and password fields.", "Make a post request to '/user/sign-up' with the provided email and password fields to test the user sign-up functionality."]}], 'highlights': ['Demonstrates enhancing node.js route files for a restful service with added authentication and implementing an mvc approach in api controllers, optimizing route files for a more efficient application structure, and implementing user sign-up and user controllers with node and express, emphasizing the importance of adding middleware for user authentication and authorization.', 'Implemented middleware checks in the route file instead of the orders controller, ensuring a clean route file and correct functionality.', 'Optimized route files for products, orders, and users, resulting in a cleaner and more organized structure.', 'Adding middleware for user authentication and authorization provided basic protection against unauthenticated users.', 'The process of creating user controllers for user creation, login, and deletion resulted in leaner routing files.']}