Handling Errors & Improving the Project Setup | Creating a REST API with Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to the serious over the last videos we already worked quite a bit on our restful api and I liked how it looked said works really great now we added some routes and zhan it's now time to improve our development set up a little bit at least and I always want to make sure that we improve error handling because we're not really handling errors in our API as of now so first of all what I want to improve regarding the setup is I don't constantly want to restart the server whenever we make a change to anything for this we can use a package called node Mon we install it with the NPM install and then - - save def because 3 is only a development dependency we need and then the name is just node Mon just download it like this this is a package which will basically monitor your files and automatically restart the server whenever you change something a link to the package can be found in the video description and there you can learn more about it so now with that note one was added I now also want you no longer manually type note server but instead with node Mon we would have to type node Mon server je s that however will fail because it's not a global package installed on our machine now we can easily work around this by going to the package.json file and adding a script let's add the start script that's a special script we can easily execute by running NPM start inside our project folder and when we type this I want to run node 1 here I can use that because this will now not search for it on our cert system but in our project and there we just installed it so then I want to execute node 1 server je s now if I run NPM start here you see we got this output here looks a little bit different than before and it essentially started our server so if I go back to postman and I try my requests again this still works because we got a no js' server but watch the console if I added something so here I'll simply add a comment routes which should handle requests and now if I save this file you see it's restarting the server even though here I didn't add any logic that would have required that but it watched the file and saw that I changed it and this all the works for files and subdirectories here if I say handle incoming get requests to slash orders and I save this it also restarted so this is really convenient now we don't have to manually cancel the server and restart whatever make a change additionally I want to add a logging package to my set up to log incoming requests so that we can also see some information here in the terminal so for that I'll temporarily quit node Mon and install another package with NPM install - - save and it's called Morgan it's a logging package for node J s we can use it really easy just go to app J s and there let's create a new constant its name at Morgan and require the name is by the way totally up to you require Morgan this name is not up to you that has to be the package you installed and now we have to basically tell Express to funnel all requests through this middleware that it is in the end and Morgan will then just log something and let the request continue so Morgan behind the scenes will call this next function saying I don't return a response I just did something in this case I locked something you continue doing your work so let's use Morgan and let's use it before we handle our requests with our routes by simply saying app use and then use some more again as a function and pass death here that's the format we want to use for the output now with that let's rerun NPM start to restart node Mon and if I now again send a request here it looked as before but you see now in a terminal we get this extra log seeing that we got a get request and how long it took and so on so that's really nice additionally I now want to handle errors keep in mind if we send a request to some invalid ID or with some invalid HTTP word we get back this default error HTML page and I want to send back JSON instead now error handling is really nice in set up here keep in mind that we have here our middleware which forwards requests to products and orders files where we then have to router on the apprehend that means if we make it past these two middlewares none of the routes set up an IVA of the two files were suited to handle the incoming requests hence we can handle errors by simply catching all requests that make it past these two requests here these two middlewares I should say so we can something use the app object and now use and no longer add a filter like slash something but simply say I want to handle every request that reaches this line because if you reach that line it means no route in one of these two files was able to handle the request so here in this middle of where I again have my request response and the next function and now I simply want to return an error here so I will create a new constant error and create a new error here the error object is available by default you don't need to import it or anything like that and here I want to output not found as a error text I will also set a error status code excuse me that should be error so that error object is provided by no chase and I can set a status code of 404 because here we get here if we have an error because we didn't find a fitting route which is the definition of a 404 error then I will actually execute this next method and pass my error along with it this will now forward the request but forward this error request here instead of the original one essentially now add another middleware here where I also get my request response and next but I get a separate first argument which actually is my error and this here will handle all kinds of errors like the error we create here and we for word or and that is why I chose this setup errors thrown from anywhere else in this application this shouldn't really be able to happen yet but later when we add a database for example and when we have operations doing work on the database these operations could fail and we then want to return a 500 error now if these operations fail they will directly throw an error so we don't make it into this first middleware but since they throw an error this middleware here would be triggered and it would handle this error or get this error so here we can set a status code on the response we eventually want to send back which is either the status code the error has like it has if we created our 400 error 404 error or we assign a 500 error code for all other kinds of errors and then we can simply return some JSON data here and we could have changed that as I did in the router setup so that would have worked too and there I want to send an error object which maybe should have a message property and this setup is totally up to you set it up as you want and there I will pass on my error message like the message this should also be error by the way the a message of the error we get as a first argument here the message will automatically be not found and for other errors whoops fix the typo for our errors these typically also have a message property it's kind of what most errors have you can pretty much rely on them having a message object a message property and now we will return this so let's try this out let's save this and no daman will restart and now let's again send an invalid request now you see we get back a method and our answer here error status is not a function it nicely proves that the automatically thrown error has a message property because that's not our own error and I did indeed is up here we should assign an error status property of 404 not a method so now let's try this again and now we get not found which again is our custom message this one and so I accidentally but it's also kind of good I guess proved that though our custom 404 and ler SD I handle all other kinds of errors like my error with the unknown function here both work and with that we got automatic reloading we got default logging and we got error handling which is a great improvement to our project with that let's leave it as this and let's go back into our project and make sure that we add more useful functionalities to it in the next videos
Info
Channel: Academind
Views: 184,806
Rating: undefined out of 5
Keywords: node, nodejs, rest, restful, rest api, tutorial
Id: UVAMha41dwo
Channel Id: undefined
Length: 10min 17sec (617 seconds)
Published: Tue Dec 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.