Now in the last lesson, we looked at how we can use this app.get method, and using a callback function, 1
we can tap into the request that was made to our server, and we managed to log it into our console, and 2
then we looked at how we can send back a response that will get rendered on our browser. 3
Now in this lesson, I want to talk more about this first parameter for our app.get method. 4
Now you can see here we're targeting the home route. 5
Now it's a little bit awkward, because in British English we pronounce route, as in R O U T E, as route, 6
but that can also sound like root as in R O O T. 7
So from now on I'm actually going to use the American pronunciation, which is very odd for me, but it 8
might help you differentiate when I'm trying to say root or when I'm trying to say route. 9
So from now on, when I say root I mean R O O T, when I say route I mean R O U T E. 10
So I hope that makes it easier for you guys to figure out what it is I'm trying to convey. 11
So this first parameter specifies the route that we are going to respond to. 12
So that means when we get a get request from a browser that is targeting this route, which is the home 13
route, then we're going to respond with this callback. 14
Now we can respond to different routes. 15
So for example, if I create another app.get method, and, instead of targeting the home route, I target maybe the 16
contact route, 17
so that simply is /contact, 18
then I can create a callback that has a req and a res, request and a response, 19
and I should really change this 20
now that I've changed the parameter to res, and in this case our response is again going to be to send some 21
information. And the information I'm going to send is simply, “Contact me at angela@gmail.com”. 22
So now we've created another route. 23
Now, as you can imagine, a lot of programming is all about forward planning. 24
So it's kind of saying like what if somebody tried to go to our home page. 25
Well then we're going to respond with this. 26
Now what if somebody tried to go to the contact page. 27
Well then in that case we'll respond with this. 28
So if I hit save now, and I again quit my server, restart my server, and we go on to port 3000, 29
then you can see that when I go to port 3000, this is my home route that I'm hitting up. 30
It's making the request to this location. 31
Now if I go to my contact route, so /contact, and I hit enter, then you can see I'm hitting 32
up a different route, 33
and that gets caught by these lines of code. 34
And in that case my server sends back the information where it says, “Contact me at angela@gmail.com”, 35
and that's what we see on our web page. 36
So you can specify in this scenario what should happen, in this scenario what should happen, and you can 37
start setting up the code for a lot of different routes. 38
So now here's a challenge. 39
I want you to create a new route so that when I go to the About page of my web site at localhost, I want 40
to be able to see a quick brief bio for who you are, 41
right? 42
So this is like a personal page essentially. 43
The home page just says, “Hello, world”, the contact page has my contact details, and then when I go to the 44
About page it should now have a brief bio of who owns this web site. 45
So go ahead and set up a new route for the About page and send back some information about yourself. 46
So pause the video now and complete that challenge. 47
All right. 48
So we've already set up two routes, one for the home route and one for the contact route. 49
So now we're going to set up a new one. 50
And, again, we're going to target get requests that are made to the about route. 51
And when that does happen, we're going to use a callback function with a request and a response. 52
Now the response we're going to make is, again, we're going to send some information, and we're going to 53
say, “My name is Angela and I love beer and code.” 54
So now our server is able to respond to three routes. 55
So if the user goes to our home page, this happens, if the user goes to our contact page, 56
this happens, 57
and if the user goes to the About page, then this should happen. 58
So let's hit save, let's stop our server, 59
start our server with the new code, and refresh. 60
Now our About page has a response from our server. 61
When we go to localhost:3000/about, we get back, 62
“My name is Angela and I love beer and code.” 63
So you can set up basically as many routes as you wish, and that means that you can start building up 64
your web site to have a lot of different pages. 65
For example, you can have a page about your hobbies, you can have a page about your CV, and you can see that 66
this is a lot easier than creating a new index.html, 67
about.html, a contact.html, that we were doing in the earlier modules when we're 68
relying only on HTML. 69
Now, just before I end this lesson, I want to show you something that I've kept to myself until now. 70
Now I hope by this point you've realized that it's pretty painful to constantly having to end your server 71
and restart your server every single time you add some new code to the 72
server.js. 73
Now if you head over to nodemon.io, this is a utility that you can install using NPM that will 74
monitor for changes in your source code, and it will automatically restart your server when it detects 75
any changes in your code. 76
So this will save us from insanity, and all you have to do is just go into your Hyper Terminal, and type 77
these commands ‘npm install -g nodemon’. 78
Now it doesn't matter where you install this. 79
It doesn't have to be inside your package. 80
It can be in your root or in your home directory. 81
Once you've installed it, it will be available across all of your projects. 82
So let's head over to our terminal, and we can say ‘npm install -g 83
nodemon’, and hit enter, and it will take a little while to install, and now when it's done we can go to 84
the location where our server.js resides, which happens to be my-express-server, 85
and then we can simply say ‘nodemon’ 86
and then the file name of our server, which happens to be server.js. 87
So now, once I hit enter, Nodemon activates, and it will start monitoring for changes. 88
So if I go in here and I add another route, say app.get, let's say this is my hobbies page, and I have 89
a callback, req, res, and we res.send maybe a ul that has a li, say something like coffee, 90
code, 91
and beer. So now that I'm done with my code, we need to watch over here. When I save my file here, 92
so my shortcut is Command S, yours might be different depending on whether you're on Windows or Mac, 93
but when I save this server.js file, then Nodemon will restart automatically, and it will keep to the port 94
that we've specified in our server. 95
So that means that my server is active and refreshed to the latest version of the code at all times, 96
and I don't have to go through that painful process of Command C, restarting. It gets a little bit 97
tiring after a while. 98
So that is Nodemon. 99
It will save your sanity and it's a really really cool tool to use. 100
So, in this lesson, we learnt all about routes that we can set up to preempt what the user might try to 101
navigate to using their browser. 102
Now, in the next lesson, we're going to be building a calculator web site, and we're going to use what 103
we learnt in these lessons so far to build a full web site. 104
It's going to look pretty simple but it's going to work great. 105
And in the process we'll see how we can get our server to do a lot of the code processing and make all 106
of the code run on our backend rather than on our front end. 107
So for all of that and more, I’ll see you on the next lesson.