All right 1
guys. 2
Welcome back. 3
Now in the HTL module, we explored how to create and use HTML forms. 4
Now in this lesson, armed with our knowledge of Javascript, Node, Express, we're going to put it to use 5
in our web site, and we’re going to use the data that gets entered into the forms, and perform calculations 6
on it in our server. 7
So first things first. We’re going to create a new file. 8
So inside the Calculator directory, I'm going to create a new file called index.html. 9
So there is our brand new HTML file, and I'm going to use the html shortcut to create our HTML boilerplate. 10
And I'm just going to call this page ‘Calculator’. 11
Now inside the body we're going to include a form. 12
Now this form is not going to have a class, but it will have an action and a method. 13
So I want you to keep the method as post and the action as index.html. We’re going to explain really shortly 14
just what those things are. 15
But before we do that let's add a few inputs. 16
So the first input is going to have a name of num1, 17
and it's also going to have a placeholder, 18
so that's that text in grey that tells the user what they should type in this text box. 19
And this is going to be “First Number”. And then we're going to have another input that's going to have 20
a name of num2, and the placeholder is going to be “Second Number”. 21
Now this name is going to be equivalent to like a variable name, and it's going to be the thing that 22
uniquely identifies the data that’s inside this input. 23
That's what we're going to be using and drawing on to perform our calculation. 24
Now just before our form ends, we're going to add a button, and this is going to be of type “submit”, and 25
the name is going to be “submit”, and the text in the button is simply going to read ‘Calculate’. 26
All right. So now that we've created our buttons and our form, we're going to send this entire index.html 27
file when the browser tries to access the home route. 28
Now when we use res.send, we’re sending individual bits of HTML data. 29
But if we want to send an entire web page, such as our index.html, we have to use something different. 30
So if we head over to the Express API reference, and you can see it’s organized by which part you're looking 31
for. And we're looking for the response part, and you can see it has a whole bunch of different methods, 32
for example res.send, which is what we've been using so far. 33
But there's also, if you scroll down, res.sendFile, and this transfers the file over to the browser 34
when they make a get request. 35
So, instead of saying res.send, we can say res.sendFile, 36
and inside the parentheses we're going to give a single input, which is the location of the file that 37
we want to send. 38
Now, what we've been working with so far are what are called relative file paths, so we can simply say 39
index.html, and it will go and search within the current file's location, 40
so calculator.js, and look for something called index.html. 41
Now this works a little bit differently when you have a server, because your server, at the moment, 42
even though it's hosted on our own computer, and we know exactly where this Calculator project folder 43
resides, it’s basically on our desktop, 44
but in the future, when we deploy our server into the cloud or into somebody else's computer, then we 45
have no idea what is that location. 46
So instead of just sending a relative file path, what we can do is we can use a constant that’s called 47
__dir, or directory, name. 48
So it's two underscores dirname, and this is going to give you the file path of the current file no 49
matter where it's hosted, on somebody else's computer, in the cloud, in a remote server, whatever it may 50
be. 51
And I just want to console.log this first, so I can show you what it looks like. 52
So let's hit save and let's restart Nodemon. So let's head over to localhost:3000. 53
And now, if we have a look inside our command line, then you can see it's printing out the directory name, 54
which is inside a directory called Calculator, which is inside the Desktop folder, which is inside angelayu, 55
which is inside the Users folder. 56
So this is the entire file path that it took to get to this current file. 57
So instead of using. 58
So instead of saying response.send, we’re going to use sendFile, and instead of using just simply 59
the relative file path, we're going to send the directory as directory name, and then 60
“/index.html”. 61
So what this full path will look like is it's going to be ‘Users/angelayu/Desktop/ 62
Calculator/index.html’. 63
And that should provide a path to reach this index.html file. 64
So now, if we hit save, our Nodemon updates our server, we go ahead and reload our calculator, 65
then you can see that we get all of those things that we had inside our index.html. And you can see 66
that when you update this index.html, it will update immediately over here using Nodemon. 67
So I’m just going to add a heading called ‘Calculator’, 68
I'm going to hit save, and now, if I refresh this web site, then bam, we've already got our calculator. 69
Now if this underscore underscore directory name is at all mysterious to you, 70
what I want you to do is to create a new file called 71
path.js, 72
and, inside this path.js, we're simply going to console.log the directory name. 73
And now, if we run this path.js, node path.js, you can see we get the location where it is 74
in, which is currently inside this Calculator. 75
And then we drag that path.js into our desktop, and we navigate back to our desktop, and then rerun 76
our node 77
path.js, 78
then you can see now the path is Users/angelayu/Desktop. So have a play around with that and see 79
if it makes sense. 80
It's basically just a constant that allows us to grab hold of the current file location at any given 81
time, so we can use that location and append the location of another file to it. 82
This means that it doesn't matter if we're on any server, 83
we don't have to know where this file resides. 84
We can simply use directory name and then append the index.html, or whichever file you're trying to 85
send. 86
Now in the next lesson, we're going to be making some post requests from our index.html. 87
So, once you're ready, head over to the next lesson.