It's been a while since we've done a multi-part tutorial. Now that we've all had a bit of a breather, I thought it might be interesting to dive into data modeling and database manipulation with Mongoose. This is going to, eventually, lead into another multi-parter where we talk about user authentication, but there's not much point in talking about that until we can store users! So, over the next few weeks, we're going to build a simple web app that allows us to do exactly that.
We're not going to cover installing Node.js here. There are too many different ways that people like to do it, depending on their OS and approach to development. I'll just say that there's a direct install path available by going to https://nodejs.org, but there are also ways to do it through the various package managers of your choice. And, in fact, there are even version managers that allow you to switch between versions. I recommend just running the latest Long Term Stable (LTS) version, which as of this tutorial is
We're also not going to cover installing MongoDB. There are several reasons why, but two of the best of them are "because it's already well-explained on the MongoDB site" and "because I don't know what OS you're using". If you're a Mac user, follow these instructions. If you're a Windows user, follow these instructions. In both cases, it's up to you whether you want to set Mongo up as a service, or manually run it each time you work on your project. We could also talk about using Docker, and perhaps we will someday, but not in this tutorial.
Once you have MongoDB installed and running, you're going to need a basic Express skeleton. We've covered this multiple times before in this series, so we're not going to re-tread that ground, but anyone who's new to the experience should head for JS Quick Hits 52 and follow along for a couple of tutorials until you have a basic Express instance scaffolded out. However, once you get to this line:
express --view=ejs -c sass --git express-test
Change it to this instead:
express --view=ejs -c sass --git mongo-test
By the way, I strongly recommend continuing with that entire Express tutorial before diving into this one, if you're not familiar with Express yet!
If you are familiar with Express, then you'll know we can't work with MongoDB without installing some modules. Specifically you're going to want to
cd to the directory into which you just installed your Express files and run the following command in your terminal or command prompt:
npm install firstname.lastname@example.org
We don't need to install the MongoDB package itself because Mongoose installs it as a dependency (you can verify this by looking into the newly-created
node_modules folder, which now has a bunch of stuff Mongoose requires in it). We're not quite done, as we need to install the stuff that Express requires to run, so just type:
Once you're done with that, start your server, either with
npm start or, better,
nodemon npm start (we've talked about Nodemon several times in the past, but here's more info on how to install and use it). Assuming your server doesn't throw any errors, you can navigate to
localhost:3000 in your browser to see the basic Welcome to Express page. That's a good place for this tutorial to stop. Not very exciting, I know, but we'll get into the good stuff next week.
See you then!
As always, you can download example files for each of these tutorials from the JS Quick Hits github repo.
Enjoying these quick hits? You can get them five days early by subscribing to our weekly newsletter.