Step-1:

Step-2:

Step-3:





Step-4:





Step-5:

Step-6:

Step-7:

Step-8:

Step-9:

Step-10: We are going to add controller then create controller directory with the app and also controller file "category.controller.js" using by directory command. $ mkdir app/controllers $ cd app/controllers $ touch category.controller.js



const Category = require('../models/category.model.js');

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

};

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

};

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

};

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

};

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

}; we are implement controller function : Add new category : exports.create = (req, res) => { if(!req.body.name) { return res.status(400).send({ message: "Fields can not be empty" }); }

// Create a Category const category = new Category({ name: req.body.name, details: req.body.details });

// Save Category in the database category.save() .then(data => { res.send(data); }).catch(err => { res.status(500).send({ message: err.message || "Something went wrong." }); }); };



List all category : exports.findAll = (req, res) => { Category.find() .then(categories => { res.send(categories); }).catch(err => { res.status(500).send({ message: err.message || "Something went wrong." }); }); };



Get a single category exports.findOne = (req, res) => { Category.findById(req.params.categoryId) .then(category => { if(!category) { return res.status(404).send({ message: "Category does not exist" }); } res.send(category); }).catch(err => { return res.status(500).send({ message: "Something went wrong." }); }); };



Update category : exports.update = (req, res) => { if(!req.body.name) { return res.status(400).send({ message: "Fields can not be empty" }); } // Find category and update it with the request body Category.findByIdAndUpdate(req.params.categoryId, { name: req.body.name, details: req.body.details }, {new: true}) .then(category => { if(!category) { return res.status(404).send({ message: "Category does not exist" }); } res.send(category); }).catch(err => { return res.status(500).send({ message: "Something went wrong" }); }); };



Delete category : exports.delete = (req, res) => { Category.findByIdAndRemove(req.params.categoryId) .then(category => { if(!category) { return res.status(404).send({ message: "Category does not exist" }); } res.send({message: "Category deleted successfully!"}); }).catch(err => { return res.status(500).send({ message: "Something went wrong" }); }); };

In this tutorial, we are talking about to HOW TO create a Rest API using Express js Node js with MongoDB to make a CRUD.Express.js is a framework of Node.js.It is a very lightweight framework and also easy to use.MongoDB-: MongoDB has become one of the maximum popular NoSQL databases, being used because of the backend for plenty important websites consisting of eBay, Craigslist, SourceForge and The the big apple times. MongoDB is available below the GNU Affero preferred Public License whilst its language drivers are to be had below the Apache License. There are also industrial licenses being supplied.Firstly we are going to create directoryfdsff then we install the application.After install application then we are going to create a package.json file using this command.After creating the file we are going to install dependencies using npm command if you are run npm init command then ask following things.Is this OK? yessee the application called server.jswe are installing a package then install the dependencies using the command:we are making an entry pointing this application using to touch Linux command.Weare creating a database configuration and also make a directory "config" inside the root directory of the application config file.In this step, we are creating a Model schema then create a new directory "app" inside the root folder of the application we are creating another directory of the app folder by the command.We are Add the Routes the create a "routes" directory and also create a route file "category.route.js" in the routes directory.We are add routes in server.js then project want the server for REST API .