Create CRUD Application in Express JS

Vaibhav Sharma
5 min readOct 10, 2018

Now in this medium story, we can create a simple APIs in Express JS ( Node JS Framework ). In this story, we can create a CRUD (Create, Read, Update, Delete) APIs. If you are new in the Node JS and want to learn how to create the basic server and create a basic APIs in Node JS, please read my medium story: Create HTTP Server in Node JS

Create a project in Express :

If you are new to Express framework and doesn’t know how to create a project in Express.js, please refer my medium story: Setup Basic Server with Express Framework. For creating a project we run the below command in the command prompt where you want to create the project.

npm init
Create a package.json file

Project Structure:

Project structure

Create properties.js file :

In the properties.js file, we define the port number in which the server is listening and define the URL of MongoDB, it is used for connecting backend code with the database.

Create database connectivity file :

This file holds the code of database connectivity. If you are new to MongoDB and mongoose, so refer to my medium story: Best Practice of Mongoose connection with MongoDB.

database.js

Step 1 : In the first step, we can require the mongoose, chalk and MongoDB URL from config/properties.js. Mongoose is ODM(Object Document Model) for Node.js and we require to chalk for giving colors to text on console and properties.js for getting DB URL.

var mongoose = require('mongoose');
var chalk = require('chalk');
var dbURL = require('./properties').DB;

Step 2 : After that, we declare the chalk variable depends upon various connections like connected, disconnected, terminated etc.

var connected = chalk.bold.cyan;
var error = chalk.bold.yellow;
var disconnected = chalk.bold.red;
var termination = chalk.bold.magenta;

Step 3 : In this step, we create a connection function. In this function, we can use mongoose.connect( ) function for connecting the MongoDB database and handle all the events like connected, error, disconnected and terminated.

Create a server.js file :

After that, we can start a server by below command :

node server.js
==============
nodemon server.js

There are lots of advantages of nodemon over node, please refer my medium story: Setup Basic Server with Express Framework

Running the server

Creating Heros Model :

In this file, we create a schema of Heros by the use of mongoose.schema. The schema is the organisation of data as a blueprint of how the database is constructed. In this schema, we create a blueprint of Hero which having name and description.

Step 1 : In this step, we require the mongoose package.

var mongoose = require('mongoose');

Step 2 : Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

var Schema = mongoose.Schema;

Step 3 : After that, we define the Schema( structure of document/data) in MongoDB by the use of mongoose.Schema.

var herosSchema = new Schema({
name :{
type: String,
unique : false,
required : true
},
description : {
type: String,
unique : false,
required : true
}}, {
timestamps: true
});

Step 4 : In the last step, we export the schema and later importing in dao file.

module.exports = herosSchema;

Create Data Access Object file :

In the Data Access Object (DOA) layer, we can define the function which is directly connected to the database and fetch data and save data from and to the database.

Step 1 : In this step, we require the mongoose package and hero.model.js file.

var mongoose = require('mongoose');
var herosSchema = require('./heros.model');

Step 2 : In the second step, we define all the function by the use of mongoose.statics. Statics are pretty much the same as methods but allow for defining functions that exist directly on your Model.

herosSchema.statics = {
create : function(data, cb) {
var hero = new this(data);
hero.save(cb);
},
get: function(query, cb) {
this.find(query, cb);
},
getByName: function(query, cb) {
this.find(query, cb);
},
update: function(query, updateData, cb) {
this.findOneAndUpdate(query,
{$set: updateData},{new: true}, cb);
},
delete: function(query, cb) {
this.findOneAndDelete(query,cb);
}
}

Step 3 : In this step, we can register the Schema with mongoose.model.

var herosModel = mongoose.model('Heros', herosSchema);

Step 4 : In the last step, we export the mongoose.model and later importing in the controller file.

module.exports = herosModel;

Create controller file :

In this file, we put all the business logic. In this file, we create all the function like create a hero, get a list of hero, get a single hero according to name, update the hero, delete the hero.

Give routes to Heros api with route.js file :

In this file, we create routes of rest api.

Update server.js file :

This is the updated server.js file. In this file, we added the routes of REST APIs, active body-parser and handle the errors. In this file, I added the self-explanatory comments.

API Calls :

Below are the all screenshots of all APIs call, which having REST APIs and result of these APIs.

Create Hero:

Create Hero

Get All Heros:

get a list of heros

Get Hero :

Get a hero

Update a Hero:

Update a hero

Delete a Hero :

Conclusion:

This is the explanatory medium story of how to create REST APIs in Express.js with the help of MongoDB. If you have any doubts, please mail me on vsvaibhav2016@gmail.com.

--

--