Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Implement CRUD in Node.js With MongoDB

DZone's Guide to

How to Implement CRUD in Node.js With MongoDB

You can use Node.js for your database operations. Take your database to the web with JavaScript templating engines to give you a hand on the way.

· Database Zone ·
Free Resource

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

In this post, we are going to create a CRUD application in Node.js with MongoDB as the database and EJS as the templating engine.

You can download the application from GitHub.

Before diving into this article, please check out my previous post — then you will get to know why I choose EJS as a templating engine. And in the next post, we will use Angular instead of these templating engines.

Prerequisites: There's no much you need beforehand, but you should have a handle on the basics:

Node.js: What it is, why we use it, its packages, etc.

MongoDB: Collections, models, schema etc.

EJS: JavaScript and some basics loops.

Let's start coding…

You might be worried about the project structure. There are two ways start Node.js applications — first, we can use the default express package to set up the app structure. Run `express node-crud`, and everything will be there. 

But the express option uses Jade as a templating engine by default We're using EJS, so we are going to create everything manually.

Run `npm init` and answer every question carefully — like name, description, git link (if you want to show it off).

Okay, in this case, we have to install all the required dependencies manually like, body-parser, express, mongo, ejs etc.

I like Lecrae's work, so here's one for you, "Don’t forget what you’re fighting for."

Here is our package.json dependencies:

"dependencies": {
     "body-parser": "^1.15.2",
     "ejs": "^2.5.2",
     "express": "^4.14.0",
     "mongoose": "^4.6.7"
}


Let’s look at our project structure.

Image title


Let's start with app.js. This is the starting point of our application.

In this file, we will include some mandatory packages and create our server (yes server, because that's where Node.js comes in). So, we'll give a port number where our application will run in the browser.

var express = require ('express');
var todoController = require('./controllers/todoController');
var app = express();

//setting template engine
app.set('view engine','ejs');

//setting assets/static files
app.use(express.static('./public'));

//Firing controllers
todoController(app);

//listen to port
app.listen(3000);
console.log("Server started at port number: 3000");

*The views created using EJS have the .ejs extension.

  • todoController contains the routes need to display particular views.

  • Listen is used to let the server listen to a particular port.

See? Easy.

We created todo-list.js to write AJAX calls. We're using simple JavaScript AJAX syntax:

  $.ajax({
        type: 'POST',
        url: '/todo',
        data: todo,
        success: function(data){
          //do something with the data via front-end framework
          location.reload();
        }
      });


Now it's time to clone the project and go through the files I mentioned, and you will be able to get it going.

As you can see, I commented above every function in the code to explain what it is, why it is there, and how it works.

Compliant Database DevOps and the role of DevSecOps DevOps is becoming the new normal in application development, and DevSecOps is now entering the picture. By balancing the desire to release code faster with the need for the same code to be secure, it addresses increasing demands for data privacy. But what about the database? How can databases be included in both DevOps and DevSecOps? What additional measures should be considered to achieve truly compliant database DevOps? This whitepaper provides a valuable insight. Get the whitepaper

Topics:
crud ,nodejs ,mongodb ,database

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}