Firebase Cloud Function Tutorial - REST API Part 2 | Diligent Dev

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on everybody it is rob aka the diligent dev and welcome to part two of our firebase cloud functions rest api if you haven't watched the first video i'll link it on the screen somewhere right now but today in this tutorial we are going to be refactoring some of the code that we wrote in part one because if you can look at some code you wrote a day a week a month ago and you can't find places where you can make it better then you should probably retire so let's head over the computer and get right into it okay so here we are back at the project and the first thing we're going to do is go ahead and enable coors i don't know if you noticed in part one but we did initialize this variable and we never used it so let's tell express to go ahead and use course so we'll say app.use course and we'll pass it a property of origin and set that equal to true and that way when we're making actual web requests and not just from postman we won't get any coors errors now the next thing we're going to do is make one instance of the firestore so right underneath we're just going to say cons db equals admin dot firestore and that way we can go through here and kind of clean up some of this code so instead of saying initializing admin dot firestore every single time we can just go through and remove those and replace them with db and then we'll go ahead and save this all right so the next thing we're going to do is remove this hello world function because it's not really doing anything for us and it was just an example of what we can accomplish with firebase functions so we'll just go ahead and remove that from our file now the next thing i'm going to do take all of our user calls and split them out into a separate users.js file this isn't necessary for our example but if you're going to have a rest api with a ton of different calls you're going to want it to be more maintainable because your index.js file is going to keep growing and growing and growing until it becomes unmaintainable so what i'm going to do here is i'm going to create a new folder in our functions and call it controllers and then i'm going to another make another file in here and call it user.js and i'm going to come back to our index.js and i'm going to copy everything out of here and put it in our users.js file and then i'm going to come back to index.js i'm going to delete everything then i'm going to type in module dot exports set that equal to an object do dot dot require for the spread operator controllers slash user and what that's going to do is go ahead and import our file and as our rest api grows we can add more and more files to this controllers folder and that'll help us make our project more maintainable the last thing we want to do is go into this users file we want to make this user app and i'm going to go ahead and replace all of this so we have no conflicts when we merge all of them in our index.js file and then we'll go ahead and run our firebase deploy script and i will be right back after that is finished okay so our deploy is finished and our hello world function was successfully deleted and our users functions were successfully updated so it's going to go ahead and wrap it up for our refactor here on our firebase functions rest api if you got any value out of this video go ahead and smash that like and subscribe button it helps out my channel and if you have any questions comments or concerns you can drop a line in the comment section underneath the video until next time happy coding [Music]
Info
Channel: Diligent Dev
Views: 7,525
Rating: undefined out of 5
Keywords: firebase, firebase functions, firebase cloud functions, api, rest api
Id: Oqj66xJZlgA
Channel Id: undefined
Length: 3min 55sec (235 seconds)
Published: Sun May 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.