00:03:46 Then after I get back an object with several information blocks or with some info on this url
00:03:54 and I'm interested in the path name which would just be, well either just slash or slash login or whatever the user entered as a route.
00:04:04 Then I can create a switch statement here where I say switch the path and then we get the case that is just a slash through the root directory.
00:04:15 In this case I want to show the index.html file, now I can do this but again reading it in with my file system module and then outputting it in the response and so on.
00:04:31 And I will do this this way but I will also do it the same way when we're having the login route and I'm rendering the login html file.
00:04:40 Therefore I will create a new function to do this, which we will call render.html, which will take a path and response as arguments.
00:04:50 Inside this function here I'm just going to copy the code from the last section here, in my file system.
00:05:00 And paste it in here. Of course I have to require my file system here.
00:05:07 And then I will just replace this index.html string here with a path … and the rest is fine. The rest is absolutely fine and it's exactly working the way we need it to work.
00:05:20 So back here in my switch statement I can then say, ok if we're accessing the root directory I want to render HTML and here I want to, well, show the index.html file
00:05:31 so I'm specifying the path to this, with the dot slash at the beginning. I also have to pass the response of course.
00:05:40 Then I break and the next case will be that I have /login and under this circumstances I want to also render in the html file but it will be well the login.html file
00:05:52 set to response and break and I then I will have the default case if the user enters anything else then I want to show, well, page not found.
00:06:04 So then I will change my HEAD to say 404, no, 404 page not found, and then I will output a response where it should say ehh route not defined.
00:06:20 And here we have to end the response because when we're rendering HTML we're doing it in the renderHTML function but here we're not doing it anywhere therefore…
00:06:28 I have to do it here. So let me save this and now it will almost work but there is one thing we haven't implemented yet and that is, I have my function here
00:06:58 And then here I can just call app and request, without parentheses.
00:07:05 This is how we create this and now let's test it. Let me start my server here.
00:07:13 And then navigate here. It works, this is the index.html because this is our root route here just with a slash in the end.
00:07:21 So let's check this, it works. Ok, that's correct. Now let's try slash login, uh, it's also working.
00:07:29 Let's try anything else, like 'any' — route not defined. Great.
00:07:34 So this is how we can implement routing in node.js, and I want to say straight away at this point I'm doing it with pure node.js so you can get the basics.
00:07:46 So you understand the way node.js works. We will very soon use express, which is a framework which will make a lot of these things we're doing by hand a lot easier.
00:07:56 Like for example writing all this routing code here is not really convenient right, and if we want to change something that's…it's just not convenient and…
00:08:07 we've got much better ways to do this when using an actual framework on top of node.js but I find it very important to understand basics too.
00:08:16 So that's it on routing, see you on the next videos. Bye.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.