- [Narrator] Now the fun begins.
And we'll start by creating our initial server code
and making sure it is running properly.
We'll simply work on the basis of our server,
which we'll add more and more as we build our bike in APS.
So I'm back here in Visual Studio Code
and what I'm going to do first
is install a few more dependencies.
So let's go ahead and do npm I and the first one
that I'm going to install is body parser,
so this package will allow us to parse
the json http request into our server
and make sure that it is running properly.
And then we're going to install also nodemon.
So now that we have those two installed on our system,
we're going to change the initial script.
So instead of test, we're going to do start,
and we're going to change the script to something else.
So let's go and highlight all of this
and we're going to change the script to nodemon index.js
and I'm going to explain in a second what this is going to do.
- E js, so this is the initial script that will allow us
to run our server and actually make sure
that babel transpiles the code before it actually runs it.
So it's going to go inside of the main file
that we're going to create momentarily,
and then execute it through a babel transpile
and then run it.
So once we do npm start as the script here,
it's going to go through this command here
and run the server.
All right so now that we've done
everything that we need to do here,
let's make sure that we save this file.
And then we'll create a brand new file,
which will be our starting point for our server.
So let me go ahead and create that new file here.
So I'm going to click on New File,
and I'm going to call this one index.js.
And this is going to be the entry point for our server.
So the first thing we need to do here,
is import express from express.
So as you can see here, I'm already using es6 intact,
so if I didn't have Babel to transpile the code for me,
I would need to do something like var require express,
and so on so forth.
So I prefer using the latest and greatest syntax,
and therefore that's why I'm using Babel.
Then what I'm going to do is create a variable called app
and pass an execution of express.
Then I'm going to create a port number
that I'll reuse over and over for the server
and I'm going to use the port 4000.
So whenever we need to use the port number,
we're going to use this variable instead.
And then, we're going to use app,
and then the method get.
So whenever somebody's requesting this particular address,
as a get request, this is what we're going to pass
inside of a function and what we'll respond with it.
And the response will be
send this message.
And the message you can put anything you want,
so in this case what I'm going to do is use template strings
so I can pass the port number as well.
Node and express server running on port
and then we'll pass the variable inside
our template string as port.
So it's going to pass the value of 4000 inside of our response.
If somebody's requesting through a get command
this particular address,
this is the response that we're going to send.
And then I'm also going to send something to the console
with a listen method.
And we're going to pass a port
and send a message there as well.
And do something in a console log so,
send a message to console log
and we'll do a template screen again.
Your server is running on port template strings,
pass the value of port.
Okay so now we have our index.js file completed,
let's save that.
We only need to create one more file
to make sure that our server runs properly.
Let's go ahead and create it.
So we click on the New File here,
and what we do is create a babel, so .babelrc file.
And this file will tell Babel when it's trying to transpile
what are the presets that we're using.
So let's go ahead and create the file.
So you need to open up an object.
And inside of that object, you are going to create
a section that's called presets,
which is an array of settings.
And inside of that array of settings,
we're going to tell it we'll use
environment preset and use stage zero.
And that's all we need to do for that file,
so we're going to save this and then go back here
and open up a terminal, so you can do Control Grave,
or click on View Terminal.
And then we're going to do an npm start.
And this will run the script that we have in here,
which is the script that we've defined
in our package.json file.
So let's go ahead and execute that.
And our server is running on port 4000,
so this is the command that we've requested here
to log on our terminal, and then if you open up a webpage
and run a request of this particular address,
so local host, port 4000, this is the first get call
that we're going to send.
We should get a response here,
node and express server running on port 4000.
So let's go ahead and test it.
Let's open a new window.
Local host, port 4000, and we get the response node
and express server is running on port 4000.
Okay so we got a server running,
ready to add more stuff to it, so let's move on.