Node.js API + MongoDB CRUD Operations | Part 01

Nimasha Madhushani
4 min readJan 5, 2023
Artwork by Author

This is Part 01 of the series of Node.js API + MongoDB CRUD Operations. I think definitely you are familiar with JavaScript before doing the hands-on practices on NodeJs. As the first step, you need to set up your environment.

Then open your command prompt and check whether you have installed the node to your local machine. To that, you can use node -v as below,

image by author

🔶If you need to upgrade the node version already installed, refer to this article: How to Upgrade or Downgrade the node Version?

Otherwise, you can use this link to download the node and you can install it as how you install software usually. [I recommend you to download the LTS version]

Now you can create a new folder and open that folder in VS Code. You can open the gitbash inside the newly created folder and run code . in that terminal.

image by author

Then open an integrated terminal and initiate the project using npm init

Then package.json the file will be created as below,

image by author

Then prepare the file structure to go ahead. In this app-structure.txt folder structure is finalized.

image by author

Now we have to install node modules and some packages. First, run npm install. Then,

To install these packages,

npm i nodemon express body-parser mongoose dotenv

Rather than installing like this, you can do the installation of packages separately as below.

npm i nodemon 
npm i express
npm i mongoose
npm i mongoose
npm i dotenv

Dependencies in package.json file after the installation of npm packages.

 "dependencies": {
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongoose": "^6.8.2",
"nodemon": "^2.0.20"
image by author

We will have a small clarification on the packages we installed here,🤗

  • nodemon — Install nodemon to restart the project automatically whenever the changes are made in the project directory. When you globally install it can use in future nodejs projects also.
  • body-parser — Node.js body parsing middleware. Parse incoming request bodies in middleware before your handlers, available under the req.body property.
  • express — Express provides methods to specify what function is called for a particular HTTP verb ( GET, POST, SET, etc.). json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based on the body-parser. This method returns the middleware that only parses JSON.
  • mongoose — Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.
  • dotenv — Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.

Then create a new file index.js to create the server. As the first step import the express. After that, assign the value return from the express method to start the server, called the method listen()

Passing the port number using a callback function should be done as below.

const express = require("express");
const app = express();
app.listen(3000, () => console.log("Server is started on 3000"));

In the package.json file, you can see this.

 "scripts": {
"test": "echo \"Error: no test specified\" && exit 1"

This runs a predefined command specified in the "start" property of a package's "scripts" object.

If the "scripts" object does not define a "start" property, npm will run node index.js.

 "scripts": {
"start": "nodemon index.js"

Finally, Run the application by npm start . The project will restart automatically whenever the changes are made in the project directory.

image by the author

Hey super cool….😎Now our application is running on port 3000 successfully. We will continue with Part 02. Stay tuned and If you have anything to clarify please drop a comment here.

Source code for this Blog series…😉

Link to Part 02: Node.js API + MongoDB CRUD Operations | Part 02

Ok… Then…Bye for now…!!👋👋