16.3. Creating a CRUD with NodeJS and MongoDB: Development Environment Setup

Página 87

To start creating a CRUD (Create, Read, Update, Delete) with NodeJS and MongoDB, the first thing we need to do is set up our development environment. This chapter will guide you step-by-step through the process.

NodeJS Installation

NodeJS is a JavaScript runtime platform that allows us to build scalable web applications. To install NodeJS, go to the official NodeJS website (https://nodejs.org) and download the latest version. Installation is pretty straightforward, just follow the on-screen instructions.

MongoDB Installation

MongoDB is a document-oriented NoSQL database. To install MongoDB, go to the official MongoDB website (https://www.mongodb.com) and download the latest version. During installation, be sure to select the option to install MongoDB as a service, which will allow MongoDB to start automatically when you start your computer.

Installing Visual Studio Code

Visual Studio Code is a source code editor developed by Microsoft. It is one of the most popular code editors and is extremely useful for developing NodeJS applications. You can download Visual Studio Code from the official website (https://code.visualstudio.com).

Project Configuration

Once you have installed NodeJS, MongoDB and Visual Studio Code, the next step is to configure your project. First, create a new folder for your project. You can do this using the mkdir command in the terminal:


$ mkdir my-crud-project
$ cd my-crud-project

Next, initialize a new NodeJS project using the npm init command. This command will create a new package.json file, which is used to manage your project's dependencies.


$ npm init -y

Installing Dependencies

For our CRUD project, we will need some dependencies. The first is Express, which is a framework for building web applications in NodeJS. You can install Express using the npm install command:


$ npm install express

The next dependency is Mongoose, which is a library for working with MongoDB in NodeJS. You can install Mongoose using the npm install command:


$ npm install mongoose

MongoDB Configuration

After installing Mongoose, we need to configure MongoDB. First, we need to start the MongoDB service. You can do this using the mongod command in the terminal:


$ mongod

Next, we need to connect Mongoose to MongoDB. This can be done by adding the following code to your main project file (usually index.js):


const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/my-crud-project', {useNewUrlParser: true, useUnifiedTopology: true});

This code will connect Mongoose to MongoDB running on your local machine on port 27017.

Conclusion

With that, you have successfully configured the development environment to create a CRUD with NodeJS and MongoDB. In the next chapter, we'll start building the CRUD application itself.

Now answer the exercise about the content:

What is the correct sequence for setting up a development environment to create a CRUD with NodeJS and MongoDB?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

8816.4. Creating a CRUD with NodeJS and MongoDB: Creating the NodeJS Project

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text