Now in the last lesson, we set up and we started our server, and our server was listening on the port 1
3000 for any browsers who are trying to get in touch with it. 2
Unfortunately we got an error, where our browser said, “I got in touch with your server at this location 3
3000, but I couldn't get anything back. 4
I can't display you anything because your server isn't giving me anything to display.” 5
So that's what we're going to address in this lesson. 6
Now, what you have to imagine is that this localhost:3000 is basically the same as the route of any web 7
site. 8
So it's the home page. 9
It's the equivalent of going to google.com, 10
right? 11
But in this case our domain is simply called localhost:3000. 12
So this / just represents localhost:3000/, which is the route, or the 13
home page, of our web site. 14
And, again, if I transform it to www.google.com/, then this will go to the home page 15
of Google, and that is what we call the route. 16
So when we load up a web site, say google.com, then our browser will send a request to Google's servers 17
to get some data for this location, and Google servers, when it sees that request, it will send our browser 18
a response, and that response includes the HTML, the CSS and the Javascript that’s needed to render 19
this web site. 20
Now, our server is currently not sending anything back when our browser makes the get requests. 21
So let's change that. Just above the app.listen, we're going to say app.get. 22
And this is a method that's provided by Express that allows us to specify what should happen when a 23
browser gets in touch with our server and makes a get request. 24
Now the first parameter it takes is the location of the get request. 25
So when we type localhost:3000, the get request is being sent to the route of our web site, which 26
is represented by a forward slash. 27
So this is basically our home page. 28
Now when that get request happens, we can trigger a callback function, and this callback function can 29
have two parameters: request and response. 30
So this method, app.get, defines what should happen when someone makes a get request to the home 31
route. 32
So that's the first parameter. 33
And then there's a callback function that tells the server what to do when that request happens. 34
So let's printout this request object that we get when the callback gets triggered and see what it looks 35
like. 36
So I'm just going to console.log the request, and I'm going to hit save l, and I'm going to terminate my 37
server by hitting Control C, and then I'm going to restart it by saying node 38
server.js, 39
with the changes that we've implemented in here. 40
So now let's head over to our browser and we're going to hit enter on localhost:3000 again, and you'll 41
see that we've got the same errors, but this time when we look inside our console, you can see that there's 42
a whole bunch of information that's packaged into this request which is being logged. 43
And this is all of the information that's associated with the request that was made to our server. 44
So, for example, you can see that my browser accepts the following languages, and these are the languages that 45
I'm happy to read, for example simplified Chinese or English. 46
You can also see other things such as what was the URL that I accessed when I triggered this request, 47
and it's got so much more information all about that request that was made to the server. 48
Now the second object here is the response. 49
This is the response that outcome server can make when the request gets triggered at this home location. 50
So currently our server isn't responding with anything, 51
and that's why we're getting this error over here. 52
In fact, Chrome actually tells you that localhost didn't send any data, 53
it’s got an empty response. So let's change that. 54
Let's actually give it a response. So we can tap into the response object, and we can use the send method 55
to send a response. 56
And we’re simply going to send back “Hello”. And now if I go onto the command line, I quit the previous server 57
session, I've saved the new code, and I run my server again, this time when we head over to localhost:3000, 58
you can see that we see the word ‘Hello’ in our browser. 59
And that's because when we typed in localhost:3000, we specified a location of a server. 60
So when we hit enter, the browser will go to that location and make a request to get some data back. 61
And when that request gets made at that home location, then this callback gets triggered, and we send 62
the browser a response, which is just the plain text of ‘Hello’. 63
Now that gets sent back to our browser and it renders it on screen. 64
So you don't have to just send plain text. 65
You can actually send HTML. 66
So let's try sending an h1 67
in this case. Let’s say ‘Hello, world!’ 68
And this is just h1 that's wrapped inside some quotation marks that we're going to send as our 69
response. 70
So now if I hit save, I quit my server again, and run my server with the new code. 71
And now we go to localhost:3000, we get the h1 being sent back with ‘Hello, world!’ 72
Now, in most cases, when people are working with Express and Node.js, you'll see these two parameters 73
shortened. 74
So, as we talked about before, instead of saying event you can say evt, or e, because you can specify 75
whatever you want for these names. 76
But usually what you'll see is req for request and res for response. 77
And that just makes it short but understandable. 78
And this is basically best practice for working with Express, and this is what you'll see out there when 79
you find other people's code and they are creating their Express servers. 80
Now there is quite a lot of new information in this lesson but a lot of it depends on the understanding 81
of how callbacks work. 82
And so if this is at all confusing, then I recommend you to take a look back at our previous module where 83
we talked about Javascript callbacks and higher order functions. 84
So I'll post a link to that in the resources of this lesson just so that you can remind yourself of 85
how we're able to provide an input into this callback function and how this input can get sent back 86
to us when this callback is triggered. 87
And it's a really good idea to create this yourself, mess around with it and send different responses, 88
or log the requests, and just make sure that you're comfortable with this syntax and how it works. 89
Now in the next lesson, I'm going to show you how to create other routes other than just targeting the 90
home page, so we can start creating a multi page web site really easily by adding more lines of code 91
into our server. 92
So for all of that and more, I’ll see you on the next lesson.