CRUD with Node.js and MongoDB (Part 1)

CRUD operations are the very basics a developer should learn when learning a new language because it helps a lot in understanding how the server-side works.

This is the first part of the tutorial where we will set up MongoDB with Node.js and will add some data to the mongo. If you have already done that feel free to skip to the second part where we will be building a CRUD with Node.js and MongoDB.

What is CRUD?

CRUD stands for Create, Read, Update and Delete. The server does the following actions for executing each of the following operations.

CreatePOSTMake something
ReadGETGet something
UpdatePUTChange Something
DeleteDELETERemove Something

Where POST, GET, PUT, and DELETE helps us in building Rest APIs. 

Technologies

For this tutorial, we will be using Node.js and MongoDB.

MongoDB is basically a database and helps you store all the data of your website. It is the most popular data storage of today and MongoDB was one of the first NoSQL data storages in the world. We will use both of these to implement the CRUD functionality. 

Node.js and MongoDB setup

The pre-requisite of this tutorial is that you should have Node and MongoDB installed on your system. You can install MongoDB from their website www.mongodb.com. Also, the curl command should be available on your command line. If you have already installed everything then by opening the command line and running the mongo command ensure that MongoDB is running. This will give you access to the system’s MongoDB instance.

 For checking the version of Node running on your pc run the following command

 Curl helps you in performing simple HTTP requests from the command line. 

Building a simple CRUD app with node and MongoDB

Line Now for the next step is to install the dependencies you need to install. Run the command mongodb polka –save once you hit enter you should get the following output.

This installs both the Node.js driver for MongoDB. Using this your project will be allowed to access Mongo and also the Polka HTTP server (HTTP requests will be handled using this.) 

Edit the package.json file to include a start script

  • A start script

First of all the start script will be added to the package.json file. The Start script is given below 

"scripts": {
	"test": "echo \"Error: no test specified\" && exit 1",
	"start": "node src/index" // <-- add this line
  },

Now create a /node-mongo-intro/src/index.js file, and put the contents of Listing 2 in it.

  • HTTP test in index.js

Now the coming to the second step we will create an index.js file in the src folder which we will place inside a new folder. Then in the next step, you can name this folder whatever you want, I named mine node-mongo-intro. Then type the following code  

const polka = require('polka');

polka()
  .get('/create', (req, res) => {
	res.send(`works`);
  })
  .listen(3000, err => {
	if (err) throw err;
	console.log(`> Running on localhost:3000`);
  });

Run the command npm run start. This will start the server and this server will listen on port 3000. You will get the following output once you hit enter after typing the command.

The above code will give the following output

So this is it for the first part of building the CRUD app with node and MongoDB CRUD. We will be focusing on the main CRUD development part in the next tutorial. The ground is all set for part 2. You can go to the second part using the link CRUD app with node and MongoDB part 2.

Scroll to Top