All right.
So now that we've spoken a bit about the theory, it's time to put it into practice and start building
our very own RESTful API.
So we're going to be building a Wikipedia style API.
So if you imagine we have a whole bunch of data relating to articles on an array of things.
So for example you could have a Wikipedia for all the programming knowledge you've learned in this course
and you create articles that's related to each of the topics that you've learnt so you could have an
article on the DOM or on
JQuery or on Bootstrap and you could keep your notes organized like this with your very own Wikipedia.
So once you're ready let's get started.
And the first thing we need to do is of course create the data that's in our database.
So the database that we're going to be using is again a MongoDB database
and I'm going to take this opportunity to show you a graphical user interface that is commonly used
with MongoDB which is called Robo 3T. And they used be called Robo Mongo
so if you head over to robomongo.org you'll be able to download Robo 3T for your platform
Windows or Mac.
And once you have downloaded it and you open it up,
this is what you should see.
There's a window that's prompting you to connect to a MongoDB database.
So let's go ahead and try to create a new MongoDB connection. And we're going to leave all of these
settings to the default and you'll notice that the address here is what we've been using previously
when we were coding it up in Atom when we were trying to access the location of our MongoDB on our
local system.
So that was localhost and MongoDB tends to use the port address that's 27017.
So leave everything as it is and go ahead and click save.
And now you should have this new connection that's created
and it might be named something else if you decided to change that naming of the connection.
So now let's go ahead and click connect to try and connect to it and you'll see that we get this error.
It says "Cannot connect to the MongoDB at localhost: 27017" and the error is that the network
is unreachable.
So why is this?
Well it's because we haven't started up our MongoDB server right?
So we have to go ahead and go into our command line, our hyper terminal, and do that thing where we say
mongod to spin up our MongoDB server.
So now it's waiting for connections on port 27017 and we can go into Robo 3T, click on this connection
button here and try to connect again.
And now you'll see that it's managed to connect successfully and we can close off this welcome screen.
So you can see on the left here the databases that we currently have on our local MongoDB instance.
And we've got two system admin, local and config
and then we've got blogDB from a previous tutorial.
So now let's go ahead and create a brand new database.
Now previously we've been doing all of this through the command line.
So we tapped into the Mongo shell and we would say something like "show dbs" which essentially does the
same thing as this
and it lists admin, local, blogDB and config same as over here.
Then we might have said something like use a particular db to create it.
And we did all of this through the command line.
Now the command line gives you an extraordinary amount of control.
You can literally do anything that is possible through command line which is why developers love it
so much.
It gives you a lot of freedom.
But if you wanted to do something quick and simple and you wanted to be able to see your data easily
then using a graphical user interface like Robo 3T can be quite helpful especially if you're not
quite yet familiar with the command line.
So let's go ahead and create a new database. And to do that we're going to right click on our new connection
and we're going to create a database.
Now this new database we will call wikiDB as the database for our Wikipedia API.
And you can see at the moment it has zero collections.
So let's go ahead and create a collection.
So right click on the collections folder and then click create
collection
and now what we have to give it a name.
Now remember that the naming convention for MongoDB collections are that it should be lowercase and
it should be plural.
So in our case if we had a whole bunch of articles then we would call it something like this
right?
Lowercase "a" and the word is in the plural form.
Now how do I know about those naming conventions?
Of course the documentation.
So whenever you're using a new piece of technology or something that you're learning about even a good
place to look through are some of the guides and some of their documentation.
But now we're going to go ahead and create our articles collection.
So now when I click on the dropdown list you can see I've got this collection of articles. But the articles
are currently empty.
If I right click and I say view documents you can see that it says fetched 0 Records
right?
And all that Robo 3T is doing is it running these predefined Mongo shell commands like what we did before
db.show collection or db.find and it translates that into kind of like a graphical user
interface in terms of tables and folders instead of simply plain text.
So given that we've got no documents in our articles collection we can go ahead and insert a document
which is the same as the C in our CRUD and we're going to create a brand new document.
So this document is going to have two fields
and the first field is going to be the title of the article. So you can see that when we create a new
document these two curly braces get added in by default and then all we have to do is put in the name
of the field which is gonna be title and then it's going to be a colon and then it's going to be whatever
it is we want it to be,
and in this case it's gonna be a string.
So let's say that we're going to create an article called REST and then we're going to create another
field called content.
So our articles will have a title and some content. The content is going to be "REST is short for REpresentational
State Transfer. It's an architectural style for designing APIs."
All right.
So as you can see we're putting them both in as strings and then we're separating the fields with a
comma.
So this is exactly the way that we did it when we were coding it up inside Atom or in our server
so this should be pretty familiar to you.
And it looks pretty much the same as any sort of Javascript object.
So now that we're done with our document let's go ahead and click save and now you'll see that Robo
3T isn't quite smart enough to auto refresh when we add in a new article.
So we have to go ahead and click View documents to rerun that line of command which find everything
inside the article's collection.
But if you have a look
now we have a new item in here.
So it's found one document and it's got three fields:
the id that gets automatically assigned by MongoDB, the title which is what we inserted as well
as the content.
So now let's go ahead and add a few more documents. If you want to
this is a good time to pause the video and add some of your own notes that you've learned along the
way
or alternatively if you head over to this URL github.com/londonappbrewery/Build-Your-Own-RESTful
-API you can see that we've already created the repository.
Then if you scroll down you can see that we've included some example documents that you can simply insert
in if you wanted to save time.
So that's exactly what I'm going to do.
I'm going to copy that from here and then I'm going to try and add it to my articles collection.
Now in order to do this I'm again gonna go insert document and I'm going to delete these two curly braces
and paste everything I got over from GitHub. And you can see I've got three documents in here one that's
got a title of API, another one about Bootstrap and another one about DOM.
So let's go ahead and hit save and right click to view documents again and you see we now have four
objects.
So this is going to be our very rudimentary database which we're going to build our API around. So potentially
any client can talk to our server in order to manipulate and change or retrieve data in our database.
So for all of that and more, I'll see you on the next lesson.