So let's dive into this demo application
and into networking with docker den,
and for that, first of all
I wanna briefly walk you through
this node demo application which we have here.
As I said that I'm using node as just well
decision I made here.
You don't have to use node,
but this application here which is built with node
has a couple of dependencies, axios, express, body-parser
and mongoose
and it uses all the dependencies and some custom code
to in the end build a web API.
Which basically means this is a web application
which does not return HTML code or anything like that
instead, it just returns data
and it also just wants data
when requests are sent to this API
and this web API has four so-called endpoints.
It accepts get requests to the API address
or domain slash favorites.
Post requests to the same URL
also slash favorites at the end
and then also get requests to this APIs IP
or domain name slash movies and slash people.
Now for movies and people
we use these Star Wars dummy API I already talked about
and we're going to send a naver HTTP request
from inside this node API to that API.
We don't know with which language it's build
but that also doesn't matter to us.
As I said, this Star Wars API
is not owned by me, you or by us.
It's a naver third party API.
And we're sending a request there
simply to then return the data fetch from this API
in a response sent back from our own API
to whoever requested this data.
And that's what we're doing for movies and for people.
But then we're all just establishing a connection
to a MongoDB database
and therefore you will need a running mongo database
for this to work,
I'll come back to that in a second.
And with that database connection established
in the other two endpoints,
we store data here for the post requests to slash favorites.
We store data in our database.
In this case,
we can basically bookmark our favorite movie or character
so we can store some basic data
about our favorite movie or character here.
And we stored that data in the database
by using a third party package named mongoose
which makes this Node.js to MongoDB communication,
a bit easier
and for get favorites,
I actually reach out to this database
and I fetch some data from there.
To be precise, I return all the favorites that were stored
with help of this post favorites endpoint.
That's what this application does.
Now to see it in action
I'll quickly do something which won't work on your system.
I will start this application
and it won't work on your system.
At least not like this
because you will need MongoDB to be installed for that.
Now, if you absolutely want to run this application
locally without docker first
you can visit MongoDB.com docs server
and then installation to learn how to install this
locally on your system for Mac iOS, Windows and so on.
But again, this is not required
I will just give you a first look
at what we're going to build throughout this module.
You don't need to install MongoDB
because we're going to dockerize this anyways.
With that set though
I got MongoDB installed up and running
and now here in the terminal,
I will run NPM install just like this,
not inside of a container
because I wanna show you this application
without containers first.
And with that
I can then run node app.js to start this application
it now connects to the database and so on.
And to now test communicating to that API,
I'm using postman,
which you can download for free from postman.com.
And that's simply a little tool
which you can use to send HTTP requests to web APIs.
With that installed I started postman here
and we can now send a new HTTP request
and I'll start with sending a get request
to local host 3,000
because this demo application is listening
on port 3000
and I'll send it to slash movies.
And if I click send here after a while a short while
I get back this movie's data,
and again that is simply fetched
from this Star Wars dummy API here.
Now let's say I like this movie here, "A new hope."
So what I'll do is I'll send the naver request,
post request to local host 3000 slash favorites.
This will target this endpoint here
where the data then gets stored
in my local MongoDB database.
So with that, I need to add a body to this request
and here I'll add a raw body in JSON format
because this API wants data in JSON
which is simply a text format for exchanging data
and it's the most commonly used format for that.
And here I then need to write curly braces
and then provide free key value pairs.
I need to add a name, a type,
and a URL field to my request data.
So here I add name between double quotes
then type between double quotes
and URL between double quotes
and the URL can now be found
from my response here on the first request to be sent.
Here I'm going to grab this URL
to this specific movie.
The name was also returned there
so I'm going to grab this name as well,
"A new hope" like this
add it between double quotes here
and to type here is movie.
And for my dummy application here
it has to be movie or a character for any other type
you would get an error.
So here I'll set it to movie
and if I now click send
I get back this response that this favorite was saved.
And if I now send another request
to get local host 3000 slash favorites
just like this,
I now get all the favorites that were stored
in my local database
and with local database just to emphasize this again,
I mean this MongoDB database
to which I'm connecting down there in my node code,
which is running on my host machine
at the moment, everything is running on my host machine.
I'm not running this node application
in a container at the moment.
So when I say local MongoDB database
I really mean MongoDB which I installed
on my machine before we started this module.
You might not have it installed
and therefore you can't run this code
but we're going to put it into a container anyways.
But I hope that for the moment,
it's clear what this application does
and what we can do with it.
Now let's put it into a container
and then see how we can still ensure
that everything works as intended.