All right.
So I hope you gave that a good go
and as always learning comes from struggling and not following tutorials.
But if you did get stuck and you wanted to check your solutions then this is the place because I'm going
to walk you through the entire process.
Now if you're already done and you're happy with the result and you've tested it, then feel free to skip
this video and continue on to the next part.
But first things first, let's create a new directory called Wiki-API. So inside my hyperterminal I'm
going to open up a new tab and I'm going to leave my MongoDB connection as it is
because if I close down this tab then my MongoDB connection will still be active but I'll have no way
of accessing it.
So it's a good idea to either control + C to exit or simply leave it running until you're done.
And remember whenever you close down the application just close it down tab by tab to make sure that
you're not closing some connections that you meant to keep open.
So I'm going to go and open up a new tab and here I'm going to navigate to my desktop.
Feel free to create your project anywhere you like but I'm just gonna do it here so that it's easy for
you to see and I'll move it later.
So on my desktop I'm going to create a new directory that is called Wiki-API.
Then I'm going to cd into this directory Wiki-API and inside here I'm going to initialize NPM and I'm
going to add the Y flag to basically just say yes to all of those questions that always pop up when
you initialize NPM.
So now that it's done and it's basically just added in all of the default options for all of those fields
and now I'm gonna go ahead and install my modules.
So I'm gonna write "npm i" which is short for install and remember I need to install body-parser, mongoose
ejs and express so make sure there's no typos and then we're going to hit enter for it to run its
course.
So now that's done.
Then we can create our app.js using the "touch" keyword.
And if I run an "ls -a" you can see now I have my app.js created inside Wiki-API directory
so.
So now we're up to here and we have to open up our app.js and add some server code.
Now for time sake instead of you having to watch me type 20 lines I'm gonna go ahead and just copy the
starting server code and I'm gonna paste paste it into my app.js and hit save.
So let's just review this code quickly.
We are creating four constants.
We are requiring all of those modules that we installed a little bit earlier on.
We're creating a new app instant using Express.
We're setting our view engine to use EJS our templating engine
and then we're going to use body-parser in order to pass our requests
and finally we're going to use the public directory to store our static files such as images and
CSS code.
The last part sets up our app to listen on port 3000
so we should now be able to use nodemon to spin up our server app.js and it should say that "Server
has started on port 3000." And now if you remember
if we head over to local host 3000
pretty much nothing will happen.
And it tells us that it cannot get our files at our root.
So this is because we haven't created any of our API yet.
Which is what we will do very shortly.
But as long as our server is running we don't have any problems in our console then that is pretty much
success and we can move on to the next step which is setting up MongoDB.
So in order to set up MongoDB we're going to use Mongoose and we're going say mongoose.connect
and we're going to connect to the usual MongoDB location which is mongodb://
localhost:27017 which is that default port that MongoDB likes to use and then we're going
to add a / to specify our database name which was called wikiDB.
Make sure you don't have any typos as it's quite a long string and it has to be pretty much precise
and that needs to match with the name of the database that you created inside Robo 3T. As you can see
mine is wikiDB, lowercase "wiki" uppercase "DB" and that is exactly the same.
So the next thing we're gonna do is we're going to add a property that says useNewUrlParser in
order to get rid of the errors that MongoDB likes to throw up and then we're gonna finish it off with
a semicolon.
And that line of code will allow Mongoose to connect to our local MongoDB instance.
So now that we've connected to our database the next thing is to create the schema for our articles
collection so that we can create some models around that.
So in order to create a new schema we're going to create a const called articleSchema or whatever
it is you want to call it.
And this is going to be set to have two fields:
one was called title and this is a string.
And the other one is called content
and this is also a string. And then we can create our model by saying const Article,
remember this is usually capitalized and it's usually the singular form.
And then we're going to set it to equal mongoose.model and then specify the name of our collection.
So Mongoose will automatically change this into a lowercase a and make this plural in quite a smart
way as we spoke about before.
And then all we have to do now is to just specify that article schema which we called articleSchema
and now we're going to cap it off with a semicolon
and that is us done.
So we've set up our connection to our MongoDB,
we've created our articleSchema and we've created our article model using Mongoose.
Now all of this should be intimately familiar to you by now
and if it's not make sure that you revisit and restudy the database module previously especially if
you have skipped some of those lessons. It is crucially important that you've already absorbed everything
in those modules before we continue.
So that's it.
That's setting up all of this.
And I hope that you managed to do it yourself and complete the challenge using what you learnt before,
maybe referring to some of the previous code
just to remind yourself. So now that we've got our server setup we are absolutely ready to get started
creating our RESTful API.
So for all of that and more I'll see you on the next lesson.