- [Instructor] This course comes
with two sample applications called Playground
and Todolist.
Playground is a simple Express.js web application
that uses MongoDB as its database in Mongoose,
which is a popular object document mapper.
You don't have to be a MongoDB or Mongoose expert
to take this course.
I will show you everything you need to know
in the context of authentication as we go.
Ideally, you can then apply your learnings
to any database you may choose.
To do list is a single page application
that uses the JavaScript framework Vue.js.
No worries, for this course
you don't have to know Vue.js at all,
everything is already set up for you.
But the key thing is
that Todolist will later in this course
use an API provided by playground
and there, I will show you
how to authenticate REST API calls.
At the start, we only need the Playground app
and to set it up,
I have now opened Visual Studio Code,
there I'm in the exercise files folder.
Now I'm opening the internal terminal official Studio Code
by clicking on view and then terminal.
And first I'm going to change into the Playground directory
and in there I first run NPM install
to install all dependencies for this project.
If you look into this folder
then let's open package.json
and as you should know already,
all the dependencies are listed in here.
But most importantly in line eight
you see that we are using Nodemon
to start the application during development.
Nodemon is a little tool that will reload your application
whenever you change your code.
This spares you from constantly
having to restart your application
manually while developing.
Let's look a little bit further into this application,
so let's start with the start script, it's in server bin
start.js
and in here, I basically had commented everything
throughout the whole application.
And if you scroll down you see that down there
in line 51
we are connecting to MongoDB using Mongoose
and later then inline 60,
we start listening with the web server on the given port,
which is 3000.
Everything else down here,
if we scroll through it is pretty standard.
Next let's look into the app.js file
because in app.js we are setting up the Express application
and as you see here as well,
I commented everything
so that you really know what's going on here.
Most importantly, we are using cookie sessions
and they are initialized here in line 41.
And then further down
we are setting up something that is called flash messaging.
And flash messaging allows you
to create messages that are displayed to the user
and are stored in the session.
So they will survive a reload
or a redirect on the page.
If we scroll further, we also see that in line 84
we are initializing the routing system.
I'm using ExpressRoute to form a route
and as you see this index route to here
is called as a function
and a pass in the config object,
so let's look at this index route.
For that, I'm heading into routes
and in there I open index.js
and there you see that index.js.
So this module here is actually a function
that takes a params argument.
So I can now pass down arguments
to my routing modules.
And if we scroll through in line 12
we are rendering the index page
and on line 17 we are rendering some of my account page
that we will use later.
And everything else is basically hooking up
the other routing modules to the rest of the routing system.
And if we look really quick into all these directories,
we see that there are a lot of routing files
and we will work with all of them throughout this course.
One more important things are the models,
so Mongoose relies
models that lets us communicate with the database
and there we have models for the user,
for the Todolist that we will use later
and also one model that we'll use later
to create tokens to reset a password.
One quick look into user model
and you see, this is a typically Mongoose model.
As I said before,
you don't have to be a Mongoose expert
to make it through this course,
I will explain everything that is important as we go.
We are not fully done yet,
we also have a directory services
and let's look into the services directory
and there you see a user service
and the to-do list service.
And if we open that
you see that this user service,
for instance, contains all the boilerplate methods
that you need to communicate with the database.
I did that to really take away
all that database queries from you
so that we can really focus on purely authentication
and what this is all about.
To finish this up,
we also have one directory
which is called middlewares
and in there I have a validation middleware.
And these validation middlewares are using Express Validator
to provide some sanity checks on user provided data,
because of course we are dealing with user provided data
whenever a user fills out some form.
And at the very last,
you also see there is a file lib passport.
And this file is at this point empty,
we will extend this file when we are using
the passport authentication library later.
Now I think we are ready to finally start the application
and for that, I'm now running NPM run dev,
and you see that Nodemon starts the application.
We also see the message
that we've successfully connected to MongaDB
and that we are listening on port 3000.
And if you now here get an error message,
this might mean that its either
your MongoDB server is not running
or maybe it's listening on the wrong port.
To change the connection string to the MongoDB instance,
you can go into config, index.js
and there you have on line 60,
connection string to MongoDB.
So maybe if you are using a local MongoDB
not the one in Docker,
the port that you would have to fill in here
would be 27017.
So that would be the default port of the MongoDB
but we are using Docker
and there we are listening,
as you know, on this port that we have here in this file.
Finally, now it's, let's open the browser
and head to local host port 3000
and the application starts,
you also see the message
that we have successfully connected to MongoDB.
And when we know click through it
you see that we also have the basic views
for registering a user,
for logging in a user
and also a view that gives us a form to reset the password.
Now, if we try to send such a form,
so I'm trying now to reset the password
for the user, foo@bar.com
and I click on reset password,
I get this not implemented error.
Of course, because the implementation
is what we are going to.
You get this not implemented error,
of course, because the implementation
is what we are going to do
starting with the next video.