[Intro Music]
Alright, we have just created the
very first small application, and
run it using Node.js container. And
it was relatively easy. All what we
did, we actually executed a
specific file, namely hello.js
using Node interpreter. That's it.
Now let's create a bit more
complicated application with
additional NPM package that we will
need to install into our
application locally on our
computer. And for that, we will use
command from Docker container.
Let's do that. Let's clear
terminal, and let's move to our
Visual Studio Code project first,
and here let me close hello.js
file, and let's create a new folder
inside of the 'node' folder. For
that, select 'node' folder; and click
on this folder icon and create a
new folder named 'express', like so.
And here inside of this folder,
let's create a new file index.js,
like so index.js. And now let's
open up web browser and type here
'npmjs.com'. NPM in Node.js stands
for Node Package Manager. And using
NPM, you're able to install many,
many different public open source
packages and create applications,
your applications based on those
third party packages. Let's search
here for package called 'express'. It
is very, very popular package.
Let's click on this first link,
'express'. And you'll see that this
package is downloaded more than 11
million times every week. Let's
here scroll down, and you'll see
how to create a very simple express
web server. And let's use those
commands. Let's simply copy all of
them from this page, copy and go to
our index.js file here in 'express'
folder, and paste all files like so.
And let's adjust this application a
bit. Let's type here, not 'Hello
World', let's type, 'This Express app
was executed by Node.js container
inside of the Docker', like so. Let's
press again key combination 'Alt',
Option 'Z', to remove horizontal
scrolling. And let's save the file.
And notice that our application
will now listen at Port 3000. And
that means that we will need to use
port mapping if we want to connect
to our express web server from
outside. And we will do that, of
course in this lecture. Great.
Let's now try to execute this file
using the same approach as we used
in the previous lecture. Let's go
to the terminal, and here let's 'cd'
to 'express' folder, because now we
are located in 'node' folder, we need
to move to 'express' folder, 'cd' to
'express'. Let's list files here. And
yes, I see single index.js file
here. And let's now use same
command as we used before, let me
go to this command in history, and
let's adjust it, and instead of
hello.js, let's type index.js, and
let's try to run this application
using the same command as we used
for hello-world application. Let's
press Enter. And I see error, Cannot
find module 'express'. And the reason
for that is very simple. We need to
first download this express module
from NPM; and only afterwards, run
our application. Otherwise, you will
get such errors 'module not found'.
And again, if I'll go back to this
application in the Visual Studio
Code, you'll see that we actually
'require' external module 'express'
Here's this line responsible for
that. And that's why first we need
to run 'npm install' command for our
application. And if I'll try to do
it locally on my computer using
simply 'npm install' command, I'll
get 'command not found: npm', because
there is no Node.js or NPM installed
on my local computer. And that's
why we need to somehow run 'npm
install' command using Node.js
Docker container. Question, how are
we able to do so? We are able to do
so using same approach as we used
in the previous command. In the
previous command we have overridden
default command and here was custom
command that was executed after the
start of this container. And now we
want to run 'npm install' command.
And for that we need to simply replace this
command, 'node index.js' with 'npm
install' command. But please note
that all those two options will
need to remain in place because we
need to run 'npm install', let's say
in our local folder on our
computer. Let's try that. Let me
remove 'node index.js', and type here
'npm install', and let's press ENTER.
[no audio]
Now, I see some meaningful output
after entering this command, and it
seems that node container tried to
install packages, but we see
warning, 'no such file or directory',
not able to open file 'package.json'
inside of the 'app' folder,
inside of the container. And that
actually means that we need to
first initialize our Node.js
project using 'npm init' command, and
answer some prompts. And
afterwards, node container will
create package.json file. Let's do
that. Let me clear terminal and
here let's go to this command. And
instead of 'npm install', let's type
'npm init', like so. And now we see
actually this prompt, 'This utility
will walk you through creation of the
package.json file', and
afterwards, we see prompt to name
our application or package, and
here is the default name 'app', but
afterwards, this script actually
terminated. And the reason for that
is that we have not connected to 'stdin'
of this process. And you may
guess that we need to add '-it'
options in order to be able to
proceed with this interactive
conversation with Node.js container.
Let me clear terminal and move
to this command back. And here,
let's add '-it' options, and make
sure that you add those options
somewhere before name of the 'node'
image. Let's press Enter. And now I
am able to proceed with those
prompts. I am happy with this
default application name version is
1.0.0. Let's press ENTER.
'description' is empty. Again, I'll
use the default values everywhere.
'entry point: index.js', 'test
command', 'git repository', 'keywords',
'author', 'license: ISC'. And if I'm
happy with all those settings, I
could press Enter and file package.json
file should now be created
in our local folder. Let's verify
that, 'ls'. And yes, I see actually
two files 'package.json', and
'package-lock.json'. I will not explain
in details what this 'package-lock.json'
file means; but if I'll go to
Visual Studio Code; and go to our
express application, you'll see
those files here as well. And if
I'll have a look at package.json
file, you will see this set of
actually values assigned to some
keys like name, version,
description, and so on. Great, now
we are ready to try again
installation of packages using 'npm
install' command. But important
outcome from this command is that
Node.js container has actually
created files package.json file,
and this one inside of our local
folder on our computer. And now
those files actually belong to our
application,and you're able to run
this application using any other
Node.js container. Alright, let's
now go to terminal, and let's run
'npm install' command. But actually,
we need to run 'npm install' command
along with the name of the package
we want to install into our
project. And name of the package is
'express', here it is. Let's go to
terminal, and here let's clear it
quickly, and let's run command 'npm
install express', like so. Here is
entire command that will be
executed after the start of this
container. And please notice that
each time when we run such command,
we actually start brand new
container. Let's press Enter. And
now you see that some job is
undergoing right now. And you see
now that 'express' package was
installed into our project. And now
if I go to Visual Studio Code, you
should see here, let's refresh, you
should see here 'node_modules' folder
inside of the 'express' folder. And
inside of that folder, you'll see a
bunch of other folders that
contain actually additional
packages that were installed along
with 'express' package. That's
because 'express' package itself has
a bunch of its own dependencies.
And here you see that in total 50
packages from 37 contributors were
added. Great. That's how we were able
to finally install 'express' package
into our Node.js application. And
notice that for that we have used
Node.js container, and we have
executed some 'npm' commands inside
of that container. It is very, very
important. We have not used node or
npm locally on our computer,
because it's simply absent in the
moment. Great.
Let's now try to start our
application once again using 'node
index.js' command. Let's clear
terminal, and go back to this
command, and instead of 'npm install
express', let's type 'node index.js'.
Here was our index.js file. Here it
is, and we want to execute it using
node. Let's press Enter. And we see
that we have connected actually
took Node.js process inside of the
container, and container now is up
and running. Let's open up new tab,
and here let's verify that, 'docker
ps', and you'll see that this
container is up. And now we are
actually able to verify whether we
are able to connect to our 'express'
server or not. And you may guess that
we will not be able to connect to this
port. And I'm sure that you know
the answer. We have not actually
mapped external port to this
internal port 3000. But let's
anyway, try to open up a web
browser, and here let's type
localhost:3000, like so. And
of course, I see this error, 'This site
can't be reached'. Let's now modify our
command here, and let's add also
port mapping. Let's go to this step,
Let's terminate our application.
And you see that actually this
doesn't work with this process,
because we are connected directly
to a Node.js process that at the
moment doesn't actually handle 'SIGINT'
signal. Let's go to this step,
and let's stop this container
manually using 'docker stop' command.
And let's type here 'a7', for
example. Actually, we need to use a
'docker kill' better because we will
need to wait 10 seconds because
again, SIGTERM signal is also not
recognized by Node.js process. And
finally, in 10 seconds, this
container will stop. And if I go
back here, you'll see that this
process is over. Let's now add all
support mapping; and here for that,
let's use option '-p'; and
let's say that we want to simply
run this 'Node.js express'
application using same port as was
opened by express application
itself, it is 3000. Let's go here
and type 3000 as external port.
And after colon, let's type 3000 as
internal port as well. Let's now
press Enter, and see whether we will
be able to connect to our Express
web server now. Let's go to Google
Chrome. And here let's refresh
page. And now I see message, 'This
Express app was executed by Node.js
container inside of the Docker'; and
that means that now our port
mapping works, and now our tiny
Express application works as well.
And again, for that we are required
to enter some NPM commands in order
to install express package inside
of our Express application. And
if I'll go to package.json file,
you will see additional dependency
that was added here in this file
and this 'express' package. Great.
Now let me demonstrate you how
we're able to handle Ctrl+C inside
of the Node.js application, and
let's adjust our application a bit
but let's do it after the small pause See you next. Bye-Bye.
[no audio]