Simple CRUD application using Node.js and Mongoose

In this article, we are going to develop a Simple CRUD application using Node.js and Mongoose.

Software used

Mongoose

Mongoose provides a simple and straight-forward, schema-based solution to model your application data. It has support for built-in type casting, validation, query building, business logic hooks and more. The Mongoose helps to reduce the boilerplate code that we may have to write for MongoDB.

Handlebars

When we start to develop our web application, we usually try to keep the business logic and HTML views separate. We may even want to have some common templates that we can reuse at different places. Handlebars is such one template engine which let you build semantic templates effectively with ease

We will develop an end to end web application with Node.js as our backend and Bootstrap and Jquery as frontend.

NPM Packages

After you init your project directory with npm init install below packages.

The babel and other packages are installed for ease of development. Below is our directory structure.

Configure Handlebars

For Handlebars, we need to let it know what are our layout and views folder and files. Below we are configuring the main layout and file extension we are going to use.

After this, the exported handlebar object can be used in our express app-

For handlebars, we also have to define the layouts/main.hbs –

Configure Mongoose

For our example, we are connecting to MongoDB on a cloud using free plan available at MongoDB Atlas.

Make sure you create your account there and set up some security like user/password, whitelisted IP address etc.

In the above code, you need to replace the user and password as per your environment. This will connect to the cloud MongoDB and create a test database.

The mongoose models use the available connection in your application. So you have to just import it into your app.

Define Schema and Model

A Schema is the core element in Mongoose. Each schema is mapped to MongoDB collection and defines its structure. To create a Model we need to provide the schema and name we need to use across our application.

In our example, we will define a simple User schema, with inbuilt id and some fields.

The code above defines a schema and a model named User

Routes

Now we will define routes to list our user, add a user and delete users. Our application will look like this

CRUD application using Node.js and Mongoose

You can read here about Build a RESTful API using Node.js

Add User

To add the user, we will accept a couple of fields from our application form. It will be passed as JSON data to our endpoint. We will use ID as an internally generated key.

If user is successfully saved, we will pass it back to our frontend for display.

Delete User

Before we delete and document from MongoDB, we need to find it first. In below code, we will find the User with given ID and if found we will remove it.

List Users

Whenever a new user is created or updated, we need to list it on our UI. On page load, it will simply query the MongoDB and find all Users.

You can find the real data stored on our cloud MongoDB instance-

Jquery Support

As mentioned earlier we are using Jquery to ease our frontend development. Below is a sample on how we are retrieving the listing for all user-

You can find more code and utility functions in home.js

Running the Application

To run the application, open a command prompt and navigate to project directory. Run command as

If you are running for the first time, then before you run the start command, initialize the project with

This will start the node server at port 6002 and you can access the application at url http://localhost:6002/

Conclusion

In this article, we have seen how we can develop a simple CRUD application using Node.js and Mongoose.

You can download the complete code from our GIT Repo

Download Code

Add a Comment

Your email address will not be published. Required fields are marked *