Advanced Node.js Development
上QQ阅读APP看书,第一时间看更新

Setting up the repo

Before we go any further, I do want to add version control to this project. In this section, we're going to create a new repo locally, make a new GitHub repository, and push our code to that GitHub repository. If you're already familiar with Git or GitHub, you can go ahead and do that on your own; you don't need to go through this section. If you're new to Git and it doesn't make sense just yet, that's also fine. Simply follow along, and we'll go through the whole process.

This section is going to be really simple; nothing MongoDB- related here. To get started, I am going to go ahead and initialize a new Git repository from the Terminal by using git init. This is going to initialize a new repository, and I can always run git status like this to take a look at the files that are untracked:

Here we have our playground folder, which we want to add under version control, and we have package.json. We also have node_modules. We do not want to track this directory. This contains all of our npm libraries. To ignore node_modules, in Atom we're going to make the .gitignore file in the root of our project. If you remember, this lets you specify files and folders that you want to leave out of your version control. I'm going to create a new file called .gitignore. In order to ignore the node_modules directory, all we have to do is type it exactly as it's shown here:

node_modules/

I'm going to save the file and rerun git status from the Terminal. We get the .gitignore folder showing up, and the node_modules folder is nowhere in sight:

The next thing we're going to do is make our first commit, using two commands. First up, I'm going to use git add . to add everything to the next commit. Then, I can make the commit using git commit with the -m flag. A good message for this commit would be Init commit:

git add .
git commit -m 'Init commit'

Now before we go, I do want to make a GitHub repository and get this code up there. This is going to require me to open up the browser and go to github.com. Once you're logged in we can make a new repo. I'm going to make a new repo and give it a name:

I'm going to go with node-course-2-todo-api. You can name yours something else if you wish. I'm going to go with this one to keep the course files organized. Now I can go ahead and create this repository, and as you may recall, GitHub actually gives us a few helpful commands:

In this case, we're pushing an existing repository from the command line. We already went through the steps of initializing the repository, adding our files and making our first commit. That means I can take the following two lines, copy them, head over to the Terminal, and paste them in:

git remote add origin https://github.com/garygreig/node-course-2-todo-api.git
git push -u origin master

You might need to do these one at a time, depending on your operating system. On the Mac, when I try to paste in multiple commands it's going to run all but the last, and then I just have to hit enter to run the last one. Take a moment to knock that out for your operating system. You might need to run it as one command, or you might be able to paste it all in and hit enter. Either way, what we have here is our code pushed up to GitHub. I can prove that it's pushed up by refreshing the repository page:

Right there we have all of our source code, the .gitignore file, package.json, and we have our playground directory with our MongoDB scripts.

That's it for this section. We'll explore how to delete data from a MongoDB collection in the next section.