This is the exciting bit because now we're going to create a server 😕! But don't panic, don't feel overwhelmed.
This course is not about server side code at all.
I'm just trying to show you...
remember, I'm just trying to show you the difference between using multipart and the default encoding
type.
But in order to do so, we need a server, so kind of call this a bonus.
But you don't have to fully understand how server side code works.
So in our HTML, we have seen that when we submit this form, we're submitting it to a URL, a server, on...
our localhost, at port 8000.
And right now, um, let's just open this with Live Server.
(sound: well, this is awkward)
You know what, this is actually not very nice looking, is it?
And if I just got here...
and we put paragraph tags.
I know this isn't the best way.
But, it hopefully just makes it look better.
Let's go back. There you go.
It looks a lot better.
(sound: yeah) If we now try and submit this, as you see we're going to get an error because we haven't defined...
our localhost, have we?
We haven't defined that server.
So that's exactly what we're going to do now.
So let's go here.
Right now...
I want to create a Node server. It's going to be super exciting.
Don't worry if you don't fully understand what's going on.
This is not a course about server side code, but I just want you to know what it is I'm doing.
The first step, right, is we have to create a server on our local machine that's listening on post 8000...
and we can set up any file to do this, a JavaScript file.
So let's just call ours, I don't know, echo.js.
Okay, now within Node there's one very, very key module and this module is called HTTP.
You can kind of think of a "module" as an overriding "object" that gives us access to a lot of properties
and methods.
So if we require this HTTP into our file, it just means we get access to a whole lot of things.
And one of these things is a another object called the Server object, and that allows us to set up...
a server.
So let's get into it.
The first thing I want to do is bring in that HTTP object.
We use the keyword "require" and there we go,
it really is as simple as this.
Now I want us to define our server.
We know that the host name, hostname, we defined as "localhost", so it's just going to be from our local...
machine, and the port name or the port number was 8000,
right.
If we go back to our index file, there we go. Its "localhost" and its 8000.
That's all we've done here.
Now I want to define our server.
How do we do that 🤷‍♂️?
Well, let's create a new variable, this time we're going to call it server.
We have to access this HTTP module and we put that entire module into a variable called HTTP. On that...
object, we have a method called createServer().
OK, so this is nothing I've written.
This comes purely from Node.js.
Now we're going to use the arrow syntax here, just to set up our entire server. If you don't know what...
this is, please check out my JavaScript Complete Grandmaster ♟ course.
Now, this createServer() takes two inputs, two arguments.
It takes the requests,
and when the request is finished being processed by the server, the server gives us a response back.
Okay, so a request and a response...
those are what those two are.
What's the first thing I want to do?
I want to console log a few things out.
It's always just good to know what's happening when you're coding.
So let's console log, let's use template literals...
let's create a newline, and then I want to bring in those objects, right, we've created. We've got...
this object, the request object in a variable called "req" and the response object in a variable called...
"res", so I want to access that right now.
And in order to do so, I want us to use the dollar sign $ and curly brackets {}.
That's why, or that's the benefit of using template literals.
Here we go.
I want to access our request object, and just see the method of our request.
We know we've set it as POST. If we go to our index...
the method is set to POST.
But I want to prove it.
I want us to look at the request object and I want us to access its method type. Okay, so that's the first...
thing I want to do.
And then why don't we just look at the, just to show you something else...
we can look at the request URL.
Very simple.
Very straightforward.
That's the first thing I want to console.
And then why don't we console log...
you can do a whole lot...
it's actually really interesting...
what what about the headers? We can console log out the headers. Okay,
that's something else we can do.
Now, I want to start getting a bit more detailed as to what happens when the request gets data.
What are we going to do with all the data?
So we access our request object, and that has another method called "on".
OK, and when we get data, we want to execute a function.
And we're going to take that chunk of data, and I just want to console log it out.
So let's say "BODY"...
and let's console log out each chunk.
There we go.
The final thing I want to do is when we get the response, let's just say we want to end with a message,
"Hello World". Whew, I know this might be a bit confusing to you, but again, this is not about server side...
code.
I'm setting this up very quickly, giving you a few things to look at and think about.
But we haven't quite finished.
We've defined our server. Oh, I've got two headings the same. Define our server...
hostname and port. Define our server request and response.
The final thing we need to do, is we need to use our server,
right.
We've kind of set up its structure, but we haven't yet implemented it.
We haven't told it to do anything.
So let's access our server variable that we've just defined...
and on there is another method called "listen". This is what we want. And we want it to listen on the...
port...
number we defined above, and on the hostname...
we defined above.
And, of course, the callback.
What do we want to happen here?
Well, let's just console log when this is successful, "Server" ... let's just do template literals again, Server is running...
at http://...
localhost, and just for fun, let's just check we have the right port.
Whew, okay, do you think this is going to look OK?
I think we've pretty much done, right.
That should work.
All we need to do now is run a terminal.
Let me just open it up.
So here we go.
We're in our terminal. All we need to do is use the keyword "node", and then we need to run our file. And in order to run...
the file, we just type it out into the terminal. And there we go.
We've already got servers running at localhost:8000 🥂. (sound: ooh yeah)
There's our code, right?
We're listening on the port...
and when the server is set up, and it is listening and it's all up and running, we execute this console log...
on it's callback, which basically is telling us that the server is running.
Isn't this awesome?
Anyway, well done.
If this is the first time you've ever set up a server, if you haven't even installed Node on your...
PC, don't worry.
Just watch these lectures.
Just follow along. Because we need to stick with the course...
and that is all about forms 📝, it's not about server code.
So in the next lecture, let's switch back to our form 📝, and let's start playing around with...
multipart and the default encoding type. See you now.