Instructor: In the last section
we started our server up and we've got our
skeleton index.js file available to us now.
As we're going through all the express code
in this section
the entire course that we're going
through here is a lot more
about React and Redux than is about express
and so we're gonna kind of speed through
a lot of the express stuff and focus much more
on the concepts as opposed to the syntax.
So again as we're going through here
it's gonna be a lot more about the concepts
not necessarily the syntax
and so as far as syntax goes I'm gonna kind of speed
through it a little bit because again
it's not the focus of this course.
Not to say that we won't wanna explain it at all
but I just want you to be prepared.
So we're in our index.js file here
and the first thing I want to make very clear
to you is that we do not have access
to the full gamut of ES6 syntax available right now.
At the time of writing this section
I'm using node version 5.7
and 5.7 still has a pretty hefty number
of features that are not included with this version of Node.
So for example I don't have access to import statements.
So instead of import statements
we will use the require syntax instead.
Let's see what that looks like.
All right express, require, express,
and so this is how we're going to
be doing code sharing between files.
We're going to be using the require keyword instead.
So I'm going to need to require a couple files,
couple different libraries in here.
First we'll start off with express.
I'm also going to import http and we're gonna talk
about what each of these are very shortly.
I'm going to grab bodyParser.
Let's also get access to Morgan.
Okay so these are basically libraries
that we just installed except for
I think we also grabbed mongoose as well
but we haven't required it into this directory just yet.
That's fine.
Let's go ahead and go through our app setup
and then we're also gonna do some server setup as well.
So stuff under this app setup section is all
about getting express the way we're working
that we want it to,
anything that goes underneath the server setup
is talking more
about getting our express application to talk
to the outside world.
Let's do the server setup first
because it's just a little bit easier.
The first thing we need to do is define a port
that our server is going to run on on our local machine.
So I'm gonna say const port is going to be process.env.PORT
or 3090.
So what this line here does is it says
if there's an environment variable of PORT already defined
use that otherwise use the port 3090,
and so by default we're probably gonna end
up using this 3090 right here most of the time.
Unless you've set up
your local machine to define a PORT ahead of time.
If you don't wanna use PORT 3090 here go ahead and set it
to whatever you'd like.
Just be aware that whenever I say 3090,
you're gonna be needing to use your local PORT instead.
So once we've got a PORT put together
we're going to
create an app at the top.
So we're gonna go back up to the top here.
I'm gonna create an instance of express.
So we take our express and we create an instance
which is our app.
Next back down at the bottom underneath server setup
we're gonna say const server is http
create server with app.
So the http library right here,
this is a native node library,
is library that is for working very low level
with http requests that are incoming.
What this line right here does is it says
create an http server that knows how to receive requests
and anything that comes in,
go ahead and forward it
onto our express application,
and this app right here is what we're going to
be adding functionality to over time.
Once we create the server we will then tell the server to
listen to the port we just declared right above and then
we will console.log simply Server listening on, port.
Make sure the port is outside of the quote right here.
Okay so let's go ahead
and save this and let's see how we're doing
if we try to run this file.
Back over my terminal
I'm going to run node index.js
and I now get server listening on 3090.
Cool.
Let's see what happens when we try to
visit local host 3090.
I'm gonna pull open my browser and just so it's clear
I'm gonna repeat this again.
To get access to our server or the location that our server
is running at is local host colon 3090.
So I've got localhost:3090.
So I'm gonna go there
and I get basically an error message that says cannot
get forward slash, which is totally fine.
Basically what the server is telling us right now is,
"Hey you're trying to get this forward slash route
but I have nothing to show you."
Nothing is set up yet.
So that's totally fine to get an error message right there.
So this is looking good for our initial setup.
Let's continue with our app set up in the next section.
I'll see you there.