CRUD operation in Node, Express Js and Mongoose | Node Js CRUD tutorial

In most of the applications we do the same thing and that CRUD (Create, Read, Update and Delete). Today we are going to do the same but in Node Js where we are using MongoDB as a database.                    Download Code

If you wanted to start with Node Js believe me this article will make you life very easy in Node Js & Mongo DB.

Before starting I believe you installed Node Js, NPM, Express and MongoDB.

Here is the video where I have discussed the below functionality.

The below link will help you for creating a Node Js App.

Create first node js project and change view template engine jade to ejs

So here we go CRUD operation in Node Js :

First of all I'm giving command to install all the dependencies required for CRUD operation.

For mongoose please run below command:

Express provides facility to use different different template engines for view and by default it gives 'jade'. I'm going to use 'ejs' template engine for views. To install the ejs dependency please run below command:

While deleting or updating we will need to display the message or to flash the messages after action completed for we need to install ''req-flash". But it uses session to flash the message so first we need to install the express-session.

Now edit your app.js file and include the 'express-session' and 'req-flash' like:

You also need to create database connection so here is code for that you just need to put this code into app.js :

Here 'user_crud' is our database name.

Now to change the view engine find the below code in app.js.

you just need to replace 'jade' with 'ejs' like:

Now use session and flash in app.js for flashing the messages.

Keep in mind flash should have been used before defining the routes in app.js

routes/user.js

schema/user.js

views:

Rename the view file like index.jade to index.ejs, layout.jade to layout.ejs and error.jade.error.ejs.

layout.ejs

user.ejs

 register.ejs

edit-user.ejs

Download Code

Share This:

Leave a Reply

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