Hi and welcome back.
In this lesson, we'll be discussing how
Node JS and Express JS are used together to build mobile application servers.
Node JS is a JavaScript runtime built on Chrome's V eight JavaScript engine,
while Express JS is a popular framework built on top of Node JS that provides
a set of features for building web and mobile applications.
So now let's talk about why Node JS and Express JS are used together.
Node JS and Express JS are often used
together to build scalable, high performance mobile application servers.
Node JS provides the foundation for serverside JavaScript code,
while Express JS provides a powerful set of tools and features for building web
and mobile applications and APIs on top of Node JS.
One of the main benefits of using Node JS
and Express JS together is the ability
to write server side code in JavaScript,
which is the same language used
for our client side mobile application.
This makes it easier for us to create full
stack mobile application without having
to switch between different
programming languages.
Express JS also provides a simple
and intuitive API for building mobile application servers and APIs.
It has a vast library of middleware available through NPM,
making it easy to extend the functionality of our applications.
Express JS also provides a way to organize our code using routes and controllers,
which makes it easy to maintain and debug our code.
But right now, we don't want to talk too
much about all of the fancy functionalities that Express has.
Let's just start using Express and node
together and then we're going to create a very simple server to show you how you
can run a server on your local development environment.
So the first things first,
what we're going to have to do is create some kind of a directory in which we're
going to be able to add files for our servers.
I created lesson one inside desktop servers.
Lesson one, you can call it whatever you'd like.
And once you do that,
just open a terminal inside your folder and run NPM install Express,
wait for this to complete and you're going to get the node modules and you're going
to get a package JSON where the only thing that you're going to see is Express.
Now, once we have this, we're going to have to create a server file.
So let's create Server JS.
You can call it whatever you want.
You can call it your name JS, or I could call it nata JS.
It doesn't really matter.
So you're free to use whatever name you want.
And the first thing that we're going to do here is use Express.
So let's create constant for Express and say that we are requiring Express.
Since we have it installed, it's going to be available for our use.
And then we want to create our application
that is going to use all the functionalities of Express.
So I'm just going to save this and then we're going to be starting to create
a simple server with node JS and express JS.
So now what we're going to do is add a route
to our application that is just going to use a get method
on the app object passing in a route path and a callback
function to handle incoming requests and sending back the responses.
And this is very simple.
All we need to do is say app, get
the path of our routes, let's make it the route and then we're
going to have a callback function created here with request and response arguments.
And then all we're going to do is use the response argument to send back
data from our server that is going to say hello world.
Great. So this is our basic setup for a route
that is going to use a get method to get back a response from the server.
Now let's get into creating a listener method on the app object.
For this we're going to need something called ports.
And I want to explain what a port is to you.
In computer networking,
a port is a virtual endpoint through which a client can communicate with a server.
And client here is going to be our mobile
application and server is what we're currently setting up and a port number is
used to identify each endpoint through which we can communicate to the server.
For example, web servers typically listen on port 80.
Our Metro bundler, for example,
listens on port 80 81 when we're developing mobile applications.
So when you create a server using node JS and express JS,
you need to specify which port your server will listen to for incoming requests.
And this is done using the listen method on the express JS application object.
So what we're going to do is say
that we're listening to the port number that we're going to define.
It can be any number.
Okay, so let's make it 3000 because
this is used a lot in the examples of ports and node JS.
And then once we start listening to this port, whenever we get connected,
we want to console log
server is running on port port.
Great.
So in this example, the server will listen for incoming requests on port 3000
and when a client makes a request to the server, the request will be sent
to this port and the server will process this request and send back a response.
So why do we need to specify a port?
Simply put, a server needs to know
which port to listen to in order to receive incoming requests.
If you don't specify a port,
the server won't know where to listen and you won't be able to connect to it.
By specifying a port number, you're telling the server where to listen
to so that clients can connect to it and communicate with it over the network.
Okay, great. So now we have a very basic setup and all
we need to do to run this server on our local environment is do the following.
Go to the root of your folder where you
have Server JS created and then just run Node server JS.
And you should have Node installed already
because we needed it for react native development as well.
So you'll definitely have access to node on your computer.
So just running and we're going to see that server is running on port 3000.
However, we don't really see this response appearing anywhere.
And if you really want to see that, all you need to do is go to your browser
and then just open up localhost and make it listen to 3000.
Okay? And you're going to see hello, world.
Now, you wouldn't see this message popped
up if you were to go here and change the route to your name.
For example, I'm going to write Nata here.
Let's go back actually because we don't have Nodemon running.
I'm going to have to stop my server and rerun it to see the changes.
But we can fix that in the upcoming lectures.
I'm going to introduce something more interesting to you.
So let's refresh this.
And now it says cannot get this route
because we don't have anything set up here.
But if I'm just going to type in Nata,
I'm going to see Hello World response from our server.
So this is how simple it is to set up
a basic server on your computer on your local environment.
And we're going to get into more details
and more complexities in the upcoming lectures.
Thanks so much for watching.
See you in the next video.