In this post, let’s go through all the pieces needed to set up the development environment for building a Node.js RESTful API with Express.js and MongoDB.
To build an API with any language or framework, we need to have a development environment setup at the local machine, the developer’s desktop/laptop. This setup will be including the operating system and the IDE (Integrated Development Environment) by default.
You can use any of the operating systems (Windows/macOS/Linux) available out there as per your choice and convenience. Depends on the OS of your choice, there are many IDEs available in these options, free/paid.
Each developer has his/her own choice of the IDE as well. Whatever your choice may be, for this tutorial you would need a Node.js development supported IDE.
Below are the few of the famous IDEs that you can choose from depends on your OS and you are comfortable with.
Next, with the IDE selection for developing the Node.js API, you need to install the Node.js on the development machine.
Go to the Node’s website NodeJS.org and download the Node.js installer for the platform (Windows/macOS/Linux) of your choice. The installer comes with NPM (Node Package Manager) module too, which will be needed to install further/update any dependent modules our API codebase and testing scripts would need.
You can install either the LTS or Current version of the node on your development machine for this exercise.
Once installed the Node and NPM, you can check each version with the below commands.
For the Node’s version,
node --version or node -v
For NPM’s version,
npm --version or npm -v
MongoDB is a document database where it stores the data in a flexible JSON-like document format which means fields can vary from document to document and also data structure can be changed over time.
Also, there is an excellent wrapper called ‘Mongoose‘ available to make the interaction with MongoDB very easy and simple. Mongoose is a MongoDB object modeling for Node.js.
For development purposes, it’s better to install and run the MongoDB locally on the development machine. There are two ways we can get the MongoDB up and running locally.
- Directly installing and running the MongoDB on the local machine
- Running MongoDB from the Docker image on the local machine
Install MongoDB Locally
To install MongoDB on your development machine, you would need to download the MongoDB community edition from the MongoDB website MongoDB.com.
Follow the installation instruction for the platform (Windows/macOS/Linux) that you choose to install MongoDB. Once installed and running, note down the port number as we need to be configured with our Node.js API.
The steps for installing MongoDB locally, configuring the database and bringing it up would seem cumbersome for some developers.
If you are adventurous and won’t mind getting your hands dirty, please go ahead and follow the steps as mentioned in the MongoDB website to install and configure it.
For those who want to get the MongoDB up and running quickly without much hassle may want to go with the docker approach as described in the next section.
Running MongoDB On Docker
For a development purpose, I would prefer to bring up the MongoDB on Docker. It’s straightforward and quick in my opinion.
Also, there is an additional benefit of spinning the MongoDB docker image up, which is, you can remove it as soon as you are done with your prototype or development very quickly. This approach will save a lot of space and keep your development machine clean.
Docker is available for many platforms from Windows, macOS, all flavors of Linux and also for cloud platforms like AWS and Azure. You can download the Docker Community Edition for your operating system and install it.
The latest Docker installer comes with the additional experimental tool called Kitematic which is GUI for managing the docker images and containers.
Once the docker is installed and running, open up the Kitematic and search for ‘Mongo’. You will find many public images for Mongo.
I would suggest to pick the official mongo image as shown in the picture and click create. It will download that image locally which you can start to use it right away.
As soon as the image is downloaded, it will start the MongoDB container automatically. Now, note down the access URL as highlighted in the below image.
Please check out the index page for this Node.js RESTful API development with TDD approach tutorial with all the posts in sequence in one place.
This blog post is an excerpt from the book Building Node.js REST API with TDD approach. Please check out the link for more information.