Now, that we have learnt about Express Generator,
the scaffolding tool for experts applications,
let's install Express Generator and then create a new Express application.
We'll be creating the exact replica of
the Express REST API application that we implemented in the previous module.
We'll be indeed reusing the three routers,
Express routers that we implemented in the previous Express application in
the newly generated application that we are going to
generate using the Express Generator in this exercise.
To get started, your first step is to install the Express Generator.
So, at the prompt type "npm
install -g" to install it as a global NPM module.
We'll say Express Generator and hit the Return.
Now, if you're installing on a OSX or a Linux Machine,
make sure to use sudo in front of this command.
Then install the application as a global application.
As you can see, the Express Generator version that I'm using is 4.16.0 in this course.
Now, that we have installed the Express Generator, in your terminal,
move to the Node.Js folder where you have been storing
all the various node applications that we've been developing in this course,
in the Node.Js folder.
At the prompt type express conFusionServer.
Now, if you've been following from the previous courses,
you understand why I call it conFusion and so
let's scaffold out our express application in the conFusionServer.
I'm scaffolding out without any new options and then within a short while,
the Express server will be scaffolded out.
Now, move into the conFusionServer folder and then
you would see that a bunch of files are already been scaffolded out for you.
So, let's open this project in Visual Studio Code.
Also, at the prompt type "npm
install" to install all the NPM modules
that are already included in the package.json file.
So, let's take a quick look at the package.json file to see what are
all the various NPM Node modules that are going to be installed.
Going to our application,
in the package.json file,
you see that the server with the default name
has been scaffolded out and you see a bunch of dependencies that are declared here.
So, all these NPM modules are going to be automatically installed for
you by NPM when you type "npm install" in this folder.
So, you will see the Node modules folder being created here.
Go into the terminal, type "npm install" and then let it
proceed with installing all the various NPM modules.
Once they are all completed,
then we are ready to get started with our Express application.
You would notice that the default Express application has already been installed.
So at the prompt,
if you just simply say npm start,
the default application will start up and you can
browse using your standard browser to see what this application serves up.
Go into Postman at the address block type localhost:3000 and
send the request and you will see that your server will return
this default information from the server site.
So, this is the standard Express server that is scaffolded out by Express Generator.
On the console, you'll see information like this printed here.
This is what Morgan prints out on the screen to show the requests coming in.
So, it shows the type of request to which URL
and the corresponding response that has been sent back to the client from the server.
Let's stop the Express server.
Let's initialize a Git repository and then
we'll check the Git status and you'll see that the node modules is being included.
So, let me switch over to my application and then create
a gitignore file and then in the gitignore file,
let me specify the node modules as a file that I want to ignore.
Then coming back to the terminal,
let me check the git status and you see that now
we are going to be checking in only those files that are part of our application.
So, at the prompt, type git add,
and then say, git status and you see all the files have been checked in.
So, you say, git commit -m "Express Generator".
Now, we're going to continue with this Express application.
We had already built the three routers using
the Express router in the previous Express application that we implemented.
We're going to copy over those three routers to this application that we generated using
Express Generator and also set up this application to make use of those three routers.
So, to do that,
let me go to the Node.Js folder and then,
going into the node express folder and routes,
I'm going to copy the dishRouter.js,
leaderRouter.js router.js and promoRouter.js that I have implemented
in the assignment and we'll come over to
the conFusionServer that I have just created and into the routes folder and then copy
over those three Express routers that we created there.
So automatically, now modifying the app.js file,
we will be able to support the REST API using
the Express application that we have just scaffolded out.
Paying a quick a visit to the application that has been scaffolded out.
As I mentioned in the previous lecture,
the app.js is the place where it all begins.
So, this is the standard file so you can see some of
the things that you're familiar with from the previous module.
So, we are requiring express path logger which is morgan in this case.
CookieParser which we will use in one of the later exercises and you
also see that you are importing two routes;
the index and the users,
which are already scaffolded out by Express for us.
So, we will add in the three new routers here.
So, I'll just say, var dishRouter require routes
dishRouter and then they will copy this and then also import
the promoRouter and the leaderRouter
and we will set these up to be used here.
Here you see that the index is mounted at
the slash and then the users is mounted at /users.
So, we'll say, app use
and dishes dishRouter.
It's Four O'clock.
Promotions promoRouter and leaders.
LeaderRouter. That's it.
Our Express application that has been scaffolded out is now all set up
to be a full-fledged REST API Server which will serve up for dish,
dishes, promotions and leaders.
Now, if you browse through the rest of the code,
it'll look all familiar with a few new things in there.
We will visit them as and when we require to understand a bit more details there.
So, you can see that the static server has been setup,
the logger has been set up there and some of this other- so,
this one is a global handler for errors.
We'll see how to make use of it in more detail.
And these two are additional error handlers that follow after this.
We will see how we make use of them as we implement further in this application.
So, this is where you notice all this information.
Now, you're wondering where the server itself is
configured to the local host and the port number are configured.
Now, this information is in this file called bin/www.
If you open the bin/www folder,
you will see additional information here.
So, it says, var app require.. slash/app.
So, from the bin folder,
you come up here and then,
this app.js file is required here into this;
and so, that will automatically include the express and everything.
And then, in here you're setting up
the http server here and you're setting up the port number.
You can also supply your own port number at the command line,
but at the moment we are going to use the default value, which is 3000,
and we'll set up the port number here; and then,
you see the var server http.createServer that you have seen earlier,
and the server is listening for things here.
And then, also, here are some listening for events on the server side.
We'll come back to this in one of the later exercises.
And then, down below you'll see some additional
code that sets up your server to be able to
handle errors and listen for incoming requests on the port number and so on.
At the moment, don't worry too much about this code.
We will revisit this code whenever we require,
and we will be modifying this code as and when we require in one of the later exercises.
So, that is where your server information is all set up there.
And app.js, we have already seen the routes folder there.
Index.js and users.js are two default routes that are set up already for us.
We will be using the users.js in one of the later exercises.
And in the public folder,
let's go ahead and copy the index.html and
aboutus.html from our Express application into the public folder also.
So, going back to the node-express public folder.
I'm just going to copy these two.
And then, we'll come back to the confusion server;
and then, in the public folder,
I'm going to simply paste them into place.
For the moment, these are placeholders.
Later on, we will be replacing them with other things.
So, with these changes,
our server is now fully set up to run as
a REST API Server and will support all the REST API endpoints.
So, let's start our server.
So, at the prompt,
type npm start and your server will be up and running.
Let's go to PostBin and send a few requests to this server.
Let's now send a get request to local host 3000/dishes,
and you will notice that the REST API Server is working exactly as we expect.
We send a request of delete and it will delete the dishes.
And then, let's send
a delete request to promotions and you see that it works just like before.
And we do a post to promotions
with the body fielding
and send the post request,
and you will see that it says,
"We'll add the promotion test with detailed description."
Let's send this to leaders.23,
but put request to leaders.23 and you should see that it says, "Updating leader 23.
Will upgrade the leader." So, you see that
the Express generator that generated the application and we have modified
it to support the entire REST API endpoints that
we configured in the previous version of the Express router.
Now, with this, we complete this exercise where we have demonstrated how we can
use the Express generator to build up the REST API endpoint.
Now, this is a good time for us to do a Git Kermit of the changes with the message,
"Express generator REST API."