How to build a REST API to perform CRUD operation with Node.js, Express and MongoDB with example
You’ll must have heard about CRUD(Create, Retrieve, Update and Delete). In the below tutorial, we’ll build a REST API with the Node.js, Express and MongoDB to perform CRUD operations. To interact with the MongoDB instance we’ll be using Mongoose.
Express - the most popular framework for Node.js is built with Node.js http module underneath it. It adds support for routing, middleware, view system and many more. Express is quite simple and easy to use, unlike other frameworks that reduce the flexibility to have design choices by trying to do way too much than expected.
Mongoose - the Object Document Mapping (ODM) tool for Node.js and MongoDB. It helps you to convert the document in database to objects in your code and vice versa.
There is a little recommendation, if you haven’t install MongoDB until now then please install before proceeding further and start Mongo as well.
In this Node.js tutorials, We’ll be building an application to save category. We’ll be building APIs for creating, listing, editing and deleting a category from the database. First we’ll build a simple web server and then configure the database. Then, we’ll build the category model and finally we’ll set different routes for handling all the operations of CRUD.
Also, we’ll be using the ES6 features like let, const, promises, arrow functions, etc. It will be better if you have a prior knowledge of these concepts as well as JavaScript.
Ok, let's go with the steps :
Step1: Create a new directoryIn this first step, I will create a folder using mkdir
command to start with the application.
$ mkdir node-mongo-crud // now go to directory cd node-mongo-crudStep2: Initialize the application
In this step, I will create a file with name package.json
using following command :
$ npm init
We use npm
command to install all the dependencies.
When you run the npm init command then you will ask for following things :
package name: (mongo-crud) node-mongo-crud-api version: (1.0.0) description: Building a crud api using Node.js, Express, and Mongodb entry point: (index.js) server.js test command: git repository: keywords: Node.js Express Mongodb Tutorial Article Post Database author: expertphp.in license: (ISC) About to write to /var/www/node-mongo-crud/package.json: { "name": "node-mongo-crud-api", "version": "1.0.0", "description": "Building a crud api using Node.js, Express, and Mongodb", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "Node.js", "Express", "Mongodb", "Tutorial", "Article", "Post", "Database" ], "author": "expertphp.in", "license": "ISC" } Is this OK? (yes) yes
As you can see, I have specified the entry point for our application called server.js
In this step, I will install the dependencies by running following command :
$ npm install express body-parser mongoose --save
--save
option is used to save all the dependencies in the package.json file.
In this step, I will create a entry point using touch
command in linux.
$ touch server.jsDirectory structure :
node-easy-notes-app └── node_modules/ └── package.json └── server.jsStep5 : Database configuration
In this step, I will create a directory "config" inside the root directory of the application and then create configuration file within the config directory by running following command :
$ mkdir config $ cd config $ touch database.config.jsdatabase.config.js
const mongoose = require('mongoose'); mongoose.Promise = global.Promise; // Connecting to the database mongoose.connect("mongodb://localhost:27017/demodatabase") .then(() => { console.log("Connection successfully established"); }).catch(err => { console.log('Could not connect to the database.'); process.exit(); });
In this step, create a new directory "app" inside the root folder of application and then create another directory within the app folder by running following command:
$ mkdir -p app/models $ cd app/models //Now create a file inside the "app/models" directory $ touch categories.model.js
Update the below code in categories.model.js
file :
const mongoose = require('mongoose'); const CategorySchema = mongoose.Schema({ name: String, details: String }, { timestamps: true }); module.exports = mongoose.model('Category', CategorySchema);
In this step, I will create a "routes" directory inside the app directory and then create a route file "category.route.js" within the routes directory.
$ mkdir app/routes $ cd app/routes $ touch category.routes.js
Now copy the below code in "category.routes.js" file :
//category.routes.js module.exports = (app) => { const category = require('../controllers/category.controller.js'); // Create a new Category app.post('/category', category.create); // Retrieve all Categorys app.get('/category', category.findAll); // Retrieve a single Category with categoryId app.get('/category/:categoryId', category.findOne); // Update a Category with categoryId app.put('/category/:categoryId', category.update); // Delete a Category with categoryId app.delete('/category/:categoryId', category.delete); }
In this step, we require the packages and configure the server for REST API:
const express = require('express'); const bodyParser = require('body-parser'); const dbConfig = require('./config/database.config.js'); 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 ExpertPHP.in"}); }); require('./app/routes/category.routes.js')(app); // listen for requests app.listen(3000, () => { console.log("Node JS Server running on port 3000"); });
In this step, I will create a controllers directory within the app and then create a controller file "category.controller.js" within the controllers directory by running below command :
$ mkdir app/controllers $ cd app/controllers $ touch category.controller.js
//category.controller.js will have following methods : const Category = require('../models/category.model.js'); // Create and Save a new 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) => { };
Now we will review the controller function implemented above one by one :
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" }); }); };