Okay.
So we saw Express or I introduced it.
Now let's get down to work, let's install it and let's make our first simple web app.
And you don't want to get your hopes up too much.
This web app is going to be very, very barebones, basically will be a website you hit enter or you
go to the URL and we just get a single message back saying whatever we want, congrats or hello or something.
So nice and simple.
But the difference between doing that and then responding with, I don't know, a big, beautiful website
with images and big texts and animations isn't that much as far as the code we have to write on the
back end, which is what we're focusing on now.
It's the same idea.
You respond with some some content, what you respond with, well, that's a different story.
So don't run this.
It's a big red red letter say but to install express like we've installed faker or we installed the
MySQL package, you just run NPM install and then the name of the package.
However, rather than doing this, this will work fine.
It will install.
I'm going to show you another way of using NPM.
That's a little bit nicer, a better practice, and it involves running this first command.
What does this do?
NPM and it creates what's known as a package JSON file.
So if that doesn't make any sense to you, don't worry.
Basically, when we make a new project, let's say we're working on a big node project with like a web
app and our web app might have 20 packages involved.
So we're only working with a couple right now, but it could have 20 involved rather than just installing
them individually and leaving no record of what's been installed and what version has been installed.
What we can do is create a package JSON file and it acts as like a log file of everything we install
so that if I send my code to someone else across the world or posted on GitHub, for example, all the
person has to do to run it is take that package JSON and then run a single command and PM install,
which will then go find the package JSON and install every necessary package.
So let me show you an example.
This is actually the GitHub repo for Express JS.
The framework we're about to install, which is itself it's a node package and there's a package JSON
file in it.
And if you look, there's this giant dependency section, these are all different packages that express
needs.
So when we install Express, it goes and installs all of these things as well.
So we don't have to do it right.
If this package JSON wasn't here, we would have to manually install array, flatten and content disposition
and cookie.
Thankfully, we don't even have to worry about what these do, let alone the fact that they exist.
So that's the point of a package JSON.
It's a way of storing not only the packages that are necessary, but then the version because this version
of express might be totally broken if we used 0.6 if that exists or escape HTML 0.0.3 instead of 1.0.3.
So that's all we'll say about it for now.
But to actually create the package JSON, what I'm going to do is first make a brand new folder just
so that we're working in a different folder.
And I'm going to just call this one, join us.
And that's where we'll be building the application.
So everything else we've done up until now hasn't actually been building the app.
So what I'll do is now.
CD into join us.
With the terminal down here, and it should be empty if I type.
LZ, we're now in this directory.
There's nothing there, so we're going to run this npm init command and this will just generate a new
package, start json.
However, it's interactive, so it's going to ask us for a name and what you can do is just hit enter
if you want it to just accept what its the default value is.
But if I hit enter now I'll get a message saying sorry name can no longer contain capital letters.
So let's do join us underscore virgin.
We'll say it's 1.0 of our app.
That's fine description.
We can leave it blank for now.
Entry point.
I like to change to app JS.
This is just the name of the file that we're writing our app in or the main file of our app.
We could have 20 files, but what's the start point?
Index is fine too, but I'm going to do app then I'm just going to hit Enter until we're done.
You could put author in there as well and a license.
Then you can see the basic text it generates for us, the JSON, just the data format and I'll hit enter.
Now I have a package JSON file up here.
And it's ready for us to install things.
So now what I'm going to do is run NPM, Install Express, but not on its own.
I'm going to add dash, dash, save and that will save a record of our install into our package dot
JSON.
So right now it's not here.
But if I do NPM install express dash, dash, save like that.
Takes a moment.
You'll see.
First of all, we have our node modules now inside of join us.
So we installed it in this directory, which is good.
Then in our package JSON, we now have express saying 4.1, 5.2 is our version.
And while we're here, why not also just reinstall NPM install to other packages we need for our app?
The first one is Faker and then the second one is MySQL.
You can just separate them by spaces and add dash, dash, save and then we'll just add both of them
in here too.
Not a dealbreaker.
We already have installed them on this machine, but it's nice to have them in this directory too and
also have records of them in our package json.
So now if I wanted to upload this to GitHub or something and share this with someone, I wouldn't have
to send them the node modules folder and send them the actual express directory and faker in my SQL.
I could ignore that because this is the largest part of our directory structure right now.
This I don't know how many megabytes it is, but it's a lot more than this single file or a single app
JS file.
So it's better to just have our friends or other developers download what they need remotely.
So I just send them this recipe that says, Oh, by the way, you need these three things.
Here's my code, plus the package JSON.
Then they go take package JSON and they install these things.
And the way that you do that, by the way, is running NPM install and you don't add anything afterwards.
It will try and install everything in the nearest package JSON.
All right.
So we have express installed.
We don't have an app yet, not even an app JS file, but we have the structure that we need to create
the app.