Complete CRUD Application with Node, Express & MongoDB

In this tutorial, we are going to learn how to make a CRUD application with MongoDB.

Checkpoints :

Introduction - 00:00
Getting Started - 02:36
Project Structure - 07:04
HTTP Server - 11:10
Modules - 17:31
Views - 22:53
Main Section - 31:00
Styling HTML - 37:51
Table & Form - 48:51
Styling Form - 57:27
Ejs - 01:08:03
Routes - 01:15:40
MongoDb - 01:22:49
API - 01:32:16
Create - 01:40:21
Find & Update - 01:48:56
Delete - 01: 56: 26
Getting Single User - 02:01:12
Using API - 02:06:06
Create New User - 02:12:52
Update User - 02:18:13
Delete User - 02:32:11

Download Source Code: https://github.com/akashyap2013/CRUD_Application_Node

Subscribe : https://www.youtube.com/channel/UCrG2Z0usOCCdUTAr4D1A8mw

#node #express #mongodb

What is GEEK

Buddha Community

Complete CRUD Application with Node, Express & MongoDB

Complete CRUD Application with Node, Express & MongoDB

In this tutorial, we are going to learn how to make a CRUD application with MongoDB.

Checkpoints :

Introduction - 00:00
Getting Started - 02:36
Project Structure - 07:04
HTTP Server - 11:10
Modules - 17:31
Views - 22:53
Main Section - 31:00
Styling HTML - 37:51
Table & Form - 48:51
Styling Form - 57:27
Ejs - 01:08:03
Routes - 01:15:40
MongoDb - 01:22:49
API - 01:32:16
Create - 01:40:21
Find & Update - 01:48:56
Delete - 01: 56: 26
Getting Single User - 02:01:12
Using API - 02:06:06
Create New User - 02:12:52
Update User - 02:18:13
Delete User - 02:32:11

Download Source Code: https://github.com/akashyap2013/CRUD_Application_Node

Subscribe : https://www.youtube.com/channel/UCrG2Z0usOCCdUTAr4D1A8mw

#node #express #mongodb

Coy  Roberts

Coy Roberts

1599336240

Node.js Express MongoDB Tutorial

Express is a Node.js web application framework that provides a robust set of features to develop rich web applications. Today we are going to deep dive in Node.js Express MongoDB Tutorial. If you want to know what is Node.js and why we use in server side technology, then please read my article  Why we use Node js as a server side technology

Node.js Express MongoDB Tutorial** will use the NoSQL database like MongoDB to store the form values in the database, Express as the Web framework and Node.js as a platform. We will use NPM as a package manager for our dependencies and Git for version control for our code.

Note:_ If you do not have a Node.js install, then please go to Node.js official website and download the package according to your OS._

Step 1: Initialize package.json file.

Create a project folder and go to that directory and put the following command in your terminal.

npm init

After answering all the data, in your root folder package.json file will be created. This file is a config file for our dependencies, so when we download new packages from Node Package Manager, package.json file will be automatically updated.

#node.js #express #node.js express mongodb #mongodb

Jamie  Graham

Jamie Graham

1636569900

How to Create CRUD GraphQL API with Nodejs, Express & MongoDB

In this tutorial, we'll learn How to Create CRUD GraphQL API with Nodejs, Express & MongoDB

While REST APIs have been gaining ground in the web application landscape over the past 20 years, GraphQL embodies a revival in the genre. We will immediately find out why!

#crud #graphql #api #node #nodejs #express #mongodb 

How to Create Node.js, Express.js and MongoDB CRUD Web Application

Tutorial of creating Node.js, Express.js and MongoDB CRUD Web Application from scratch using Express.js basic feature.

This is the tutorial of how to create Node.js, Express.js, and MongoDB CRUD Web Application from scratch with the basic use of Express.js. For data modeling, we will use Mongoose.js module and for UI (User Interface) or front end, we use “ejs” instead of jade that comes with Express.js project generation. Don’t waste your time, let’s get started.

Table of Contents:

  • Create Node Express Web App Project
  • Create Mongoose.js Model or Schema
  • Create Node Express Controller for CRUD Operations
  • Create Node Express Routes
  • Create Node Express Views for CRUD User Interface

#node #express #mongodb #web-development

Create a full CRUD API application with Node.js, Express and MongoDB for Beginners

In this tutorial, We will be building a simple Note-Taking application. We will build Rest APIs for creating, listing, editing and deleting a Note.

We’ll start by building a simple web server and then move on to configuring the database, building the Note model and different routes for handling all the CRUD operations.

Finally, we’ll test our REST APIs using Postman.

Also, In this post, we’ll heavily use ES6 features like let, const, arrow functions, promises etc. It’s good to familiarize yourself with these features.

Let’s get started

1. Fire up your terminal and create a new folder for the application.

$ mkdir node-easy-notes-app

2. Initialize the application with a package.json file

Go to the root folder of your application and type npm init to initialize your app with a package.json file.


$ cd node-easy-notes-app
$ npm init

name: (node-easy-notes-app) 
version: (1.0.0) 
description: Never miss a thing in Life. Take notes quickly. Organize and keep track of all your notes.
entry point: (index.js) server.js
test command: 
git repository: 
keywords: Express RestAPI MongoDB Mongoose Notes
author: callicoder
license: (ISC) MIT
About to write to /Users/rajeevkumarsingh/node-easy-notes-app/package.json:

{
  "name": "node-easy-notes-app",
  "version": "1.0.0",
  "description": "Never miss a thing in Life. Take notes quickly. Organize and keep track of all your notes.",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Express",
    "RestAPI",
    "MongoDB",
    "Mongoose",
    "Notes"
  ],
  "author": "callicoder",
  "license": "MIT"
}

Is this ok? (yes) yes

Note that I’ve specified a file named server.js as the entry point of our application. We’ll create server.js file in the next section.

3. Install dependencies

We will need express, mongoose and body-parser modules in our application. Let’s install them by typing the following command -


$ npm install express body-parser mongoose --save

I’ve used --save option to save all the dependencies in the package.json file. The final package.json file looks like this -


{
  "name": "node-easy-notes-app",
  "version": "1.0.0",
  "description": "Never miss a thing in Life. Take notes quickly. Organize and keep track of all your notes.",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Express",
    "RestAPI",
    "MongoDB",
    "Mongoose",
    "Notes"
  ],
  "author": "callicoder",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "mongoose": "^5.2.8"
  }
}


Our application folder now has a package.json file and a node_modules folder -


node-easy-notes-app
    └── node_modules/
    └── package.json

Setting up the web server

Let’s now create the main entry point of our application. Create a new file named server.js in the root folder of the application with the following contents -


const express = require('express');
const bodyParser = require('body-parser');

// create express app
const app = express();

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))

// parse requests of content-type - application/json
app.use(bodyParser.json())

// define a simple route
app.get('/', (req, res) => {
    res.json({"message": "Welcome to EasyNotes application. Take notes quickly. Organize and keep track of all your notes."});
});

// listen for requests
app.listen(3000, () => {
    console.log("Server is listening on port 3000");
});



First, We import express and body-parser modules. Express, as you know, is a web framework that we’ll be using for building the REST APIs, and body-parser is a module that parses the request (of various content types) and creates a req.body object that we can access in our routes.

Then, We create an express app, and add two body-parser middlewares using express’s app.use() method. A middleware is a function that has access to the request and response objects. It can execute any code, transform the request object, or return a response.

Then, We define a simple GET route which returns a welcome message to the clients.

Finally, We listen on port 3000 for incoming connections.

All right! Let’s now run the server and go to http://localhost:3000 to access the route we just defined.


$ node server.js 
Server is listening on port 3000

Node Express MongoDB Rest API

Configuring and Connecting to the database

I like to keep all the configurations for the app in a separate folder. Let’s create a new folder config in the root folder of our application for keeping all the configurations


$ mkdir config
$ cd config

Now, Create a new file database.config.js inside config folder with the following contents -


module.exports = {
    url: 'mongodb://localhost:27017/easy-notes'
}

We’ll now import the above database configuration in server.js and connect to the database using mongoose.

Add the following code to the server.js file after app.use(bodyParser.json()) line


// Configuring the database
const dbConfig = require('./config/database.config.js');
const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

// Connecting to the database
mongoose.connect(dbConfig.url, {
    useNewUrlParser: true
}).then(() => {
    console.log("Successfully connected to the database");    
}).catch(err => {
    console.log('Could not connect to the database. Exiting now...', err);
    process.exit();
});


Please run the server and make sure that you’re able to connect to the database -


$ node server.js 
Server is listening on port 3000
Successfully connected to the database

Defining the Note model in Mongoose

Next, We will define the Note model. Create a new folder called app inside the root folder of the application, then create another folder called models inside
the app folder


$ mkdir -p app/models
$ cd app/models

Now, create a file called note.model.js inside app/models folder with the following contents -


const mongoose = require('mongoose');

const NoteSchema = mongoose.Schema({
    title: String,
    content: String
}, {
    timestamps: true
});

module.exports = mongoose.model('Note', NoteSchema);

The Note model is very simple. It contains a title and a content field. I have also added a timestamps option to the schema.

Mongoose uses this option to automatically add two new fields - createdAt and updatedAt to the schema.

Defining Routes using Express

Next up is the routes for the Notes APIs. Create a new folder called routes inside the app folder.


$ mkdir app/routes
$ cd app/routes

Now, create a new file called note.routes.js inside app/routes folder with the following contents -


module.exports = (app) => {
    const notes = require('../controllers/note.controller.js');

    // Create a new Note
    app.post('/notes', notes.create);

    // Retrieve all Notes
    app.get('/notes', notes.findAll);

    // Retrieve a single Note with noteId
    app.get('/notes/:noteId', notes.findOne);

    // Update a Note with noteId
    app.put('/notes/:noteId', notes.update);

    // Delete a Note with noteId
    app.delete('/notes/:noteId', notes.delete);
}


Note that We have added a require statement for note.controller.js file. We’ll define the controller file in the next section. The controller will contain methods for handling all the CRUD operations.

Before defining the controller, let’s first include the routes in server.js. Add the following require statement before app.listen() line inside server.js file.


// ........

// Require Notes routes
require('./app/routes/note.routes.js')(app);

// ........

If you run the server now, you’ll get the following error


$ node server.js
module.js:472
    throw err;
    ^

Error: Cannot find module '../controllers/note.controller.js'

This is because we haven’t defined the controller yet. Let’s do that now.

Writing the Controller functions

Create a new folder called controllers inside the app folder, then create a new file called note.controller.js inside app/controllers folder with the following contents



const Note = require('../models/note.model.js');

// Create and Save a new Note
exports.create = (req, res) => {

};

// Retrieve and return all notes from the database.
exports.findAll = (req, res) => {

};

// Find a single note with a noteId
exports.findOne = (req, res) => {

};

// Update a note identified by the noteId in the request
exports.update = (req, res) => {

};

// Delete a note with the specified noteId in the request
exports.delete = (req, res) => {

};


Let’s now look at the implementation of the above controller functions one by one -

Creating a new Note


// Create and Save a new Note
exports.create = (req, res) => {
    // Validate request
    if(!req.body.content) {
        return res.status(400).send({
            message: "Note content can not be empty"
        });
    }

    // Create a Note
    const note = new Note({
        title: req.body.title || "Untitled Note", 
        content: req.body.content
    });

    // Save Note in the database
    note.save()
    .then(data => {
        res.send(data);
    }).catch(err => {
        res.status(500).send({
            message: err.message || "Some error occurred while creating the Note."
        });
    });
};



Retrieving all Notes


// Retrieve and return all notes from the database.
exports.findAll = (req, res) => {
    Note.find()
    .then(notes => {
        res.send(notes);
    }).catch(err => {
        res.status(500).send({
            message: err.message || "Some error occurred while retrieving notes."
        });
    });
};

Retrieving a single Note


// Find a single note with a noteId
exports.findOne = (req, res) => {
    Note.findById(req.params.noteId)
    .then(note => {
        if(!note) {
            return res.status(404).send({
                message: "Note not found with id " + req.params.noteId
            });            
        }
        res.send(note);
    }).catch(err => {
        if(err.kind === 'ObjectId') {
            return res.status(404).send({
                message: "Note not found with id " + req.params.noteId
            });                
        }
        return res.status(500).send({
            message: "Error retrieving note with id " + req.params.noteId
        });
    });
};


Updating a Note


// Update a note identified by the noteId in the request
exports.update = (req, res) => {
    // Validate Request
    if(!req.body.content) {
        return res.status(400).send({
            message: "Note content can not be empty"
        });
    }

    // Find note and update it with the request body
    Note.findByIdAndUpdate(req.params.noteId, {
        title: req.body.title || "Untitled Note",
        content: req.body.content
    }, {new: true})
    .then(note => {
        if(!note) {
            return res.status(404).send({
                message: "Note not found with id " + req.params.noteId
            });
        }
        res.send(note);
    }).catch(err => {
        if(err.kind === 'ObjectId') {
            return res.status(404).send({
                message: "Note not found with id " + req.params.noteId
            });                
        }
        return res.status(500).send({
            message: "Error updating note with id " + req.params.noteId
        });
    });
};


The {new: true} option in the findByIdAndUpdate() method is used to return the modified document to the then() function instead of the original.

Deleting a Note


// Delete a note with the specified noteId in the request
exports.delete = (req, res) => {
    Note.findByIdAndRemove(req.params.noteId)
    .then(note => {
        if(!note) {
            return res.status(404).send({
                message: "Note not found with id " + req.params.noteId
            });
        }
        res.send({message: "Note deleted successfully!"});
    }).catch(err => {
        if(err.kind === 'ObjectId' || err.name === 'NotFound') {
            return res.status(404).send({
                message: "Note not found with id " + req.params.noteId
            });                
        }
        return res.status(500).send({
            message: "Could not delete note with id " + req.params.noteId
        });
    });
};



You can check out the documentation of all the methods that we used in the above APIs on Mongoose’s official documentation -

Testing our APIs

Let’s now test all the APIs one by one using postman.

  • Creating a new Note using POST /notes API
  • Retrieving all Notes using GET /notes API
  • Retrieving a single Note using GET /notes/:noteId API
  • Updating a Note using PUT /notes/:noteId API
  • Deleting a Note using DELETE /notes/:noteId API

In this tutorial, We learned how to build rest apis in node.js using express framework and mongodb.

You can find the code for this tutorial in my github repository.

Originally published by Rajeev Singh at callicoder.com

#node-js #mongodb #express #javascript