- Advanced Node.js Development
- Andrew Mead
- 355字
- 2021-08-27 19:05:51
Creating a directory for the project
To kick things off, I'm going to make a new folder on the Desktop for the Node API. I'll use mkdir to create a new folder called node-todo-api. Then, I can go ahead and use cd to go into that directory, cd node-todo-api. And from here, we're going to run npm init, which creates our package.json file and lets us install our MongoDB library. Once again, we're going to be using enter to skip through all of the options, using the defaults for each:
Once we get to the end we can confirm our selections, and now our package.json file is created. The next thing we're going to do is open up this directory inside of Atom. It's on the Desktop, node-todo-api. Next up, inside of the root of the project we're going to create a new folder, and I'm going to call this folder playground. Inside of this folder, we'll store various scripts. They're not going to be scripts related to the Todo API; they'll be scripts related to MongoDB, so I do want to keep them in the folder, but I don't necessarily want them to be part of the app. We'll use the playground folder for that, like we have in the past.
In the playground folder, let's go ahead and make a new file, and we'll call this file mongodb-connect.js. Inside of this file, we're going to get started by loading in the library and connecting to the database. Now in order to do that, we have to install the library. From the Terminal, we can run npm install to get that done. The new library name is mongodb; all lowercase, no hyphens. Then, we're going to go ahead and specify the version to make sure we're all using the same functionality, @3.0.2. This is the most recent version at the time of writing. After the version number, I am going to use the --save flag. This is going to save it as a regular dependency, which it already is:
npm install mongodb@3.0.2 --save
We're going to need this to run the Todo API application.