CRUD Example using Node.js, Express.js, MongoDB
Tags: node, mongodb, crud, express
Published: 2022-08-03 17:42:00

In this post we will learn about basic CRUD operation by using Node.js and MongoDB. 
**CRUD is the fundamental concept for any web application.**

In this CRUD application, we use the Task model to create task for a to-do application.
Here the Task model will contain

- id

- title

- description

- created time

- updated time

Now, We Will create the model for Task...

```javascript
const { Schema, model } = require('mongoose')

const taskSchema = new Schema({
    title: {
        type: String
    },
    description: {
        type: String
    }
}, {
    timestamps: true
})

const Task = model('Task', taskSchema)
module.exports = Task
```

Now we will create the Server and connect server to the database.

```javascript
require('dotenv').config()

const express = require('express')
const mongoose = require('mongoose')
const http = require('http')

const setMiddlewares = require('./middleware/middlewares')
const setRoutes = require('./router/routes')



const app = express()
const server = http.createServer(app)

setMiddlewares(app)
setRoutes(app)


const PORT = process.env.PORT || 1000
const DB_USER = process.env.DB_USER
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_NAME = process.env.DB_NAME



mongoose.connect(`mongodb+srv://DB_USER :DB_PASSWORD 1@mycluster.oazue.mongodb.net/DB_NAME?retryWrites=true&w=majority`, { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => {
        console.log('Database connect success')
        server.listen(PORT, () => {
            console.log('Server Is Ready')
        })
    }).catch(e => {
        return console.log(e)
    })
```

Now, we will create the controller for create task, read task, update task and delete task.

Create Task Controller: 

```javascript
exports.createTaskController = async(req, res, next) => {
    try {
        let { title, description } = req.body
        let newTask = new Task({
            title,
            description
        })

        await newTask.save(newTask)
            .then(data => {
                return res.send(data)
            }).catch(err => {
                res.status(500).send({
                    msg: 'task create fail'
                })
            })

        console.log(newTask)
    } catch (e) {
        console.log(e)
        next()
    }
}
```

Read All Task Controller:

```javascript
exports.getTaskController = async(req, res, next) => {
    try {
        await Task.find()
            .then(tasks => {
                return res.send(tasks)
            }).catch(err => {
                res.status(500).send({
                    msg: 'no tast found'
                })
            })

    } catch (e) {
        console.log(e)
        next()
    }
}
```

Read Single Task Controller:

```javascript
exports.getSingleTask = async(req, res, next) => {
    try {
        let taskId = req.params.id
        await Task.findById(taskId)
            .then(task => {
                if (task) {
                    return res.send(task)
                } else {
                    return res.send({ msg: 'no task found' })
                }

            }).catch(err => {
                res.status(500).send({
                    msg: 'no task found'
                })
            })

    } catch (e) {
        console.log(e)
        next()
    }
}
```

Update Task Controller:

```javascript
exports.updateTastController = async(req, res, next) => {
    try {
        let taskId = req.params.id
        let { title, description } = req.body
        let task = await Task.findById(taskId)
        console.log(task)
        if (!task) {
            return res.json({ msg: 'no task found' })
        }

        await Task.findByIdAndUpdate(taskId, {
            $set: {
                title,
                description
            }
        }, { new: true }).then(data => {
            return res.send(data)
        }).catch(err => {
            res.status(500).send({
                msg: 'task update fail'
            })
        })

    } catch (e) {
        console.log(e)
        next()
    }
}
```

Delete Task Controller:

```javascript
exports.deleteTaskController = async(req, res, next) => {
    try {
        let taskId = req.params.id
        await Task.findByIdAndDelete(taskId)
            .then(data => {
                return res.send(data)
            }).catch(err => {
                res.status(500).send({
                    msg: 'task delete fail'
                })
            })
    } catch (e) {
        console.log(e)
    }
}
```
Now we create the route for all operation
route.js:

```javascript
const crudRoute = require('../router/cruduRoute')

const routes = [{
        path: '/tasks',
        handler: crudRoute
    },
    {
        path: '/',
        handler: (req, res, next) => {
            return res.json({ msg: 'Welcome to my application' })
        }
    }
]

module.exports = (app) => {
    routes.forEach(r => {
        if (r.path == '/') {
            app.get(r.path, r.handler);
        } else {
            app.use(r.path, r.handler)
        }
    })
}
```
crudRoute.js:
```javascript
const router = require('express').Router()
const { getTaskController, getSingleTask, createTaskController, updateTastController, deleteTaskController } = require('../controller/crudController')


router.get('/', getTaskController)
router.get('/:id', getSingleTask)
router.post('/create', createTaskController)
router.put('/update/:id', updateTastController)
router.delete('/delete/:id', deleteTaskController)

module.exports = router
```
Now we can run our project, and get all the output:
**Create Task:**
api:`http://localhost:1000/tasks/create`
JSON Data:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8b03yjnmpbt8qptfsqks.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0u4oz39avn73xnvmuryz.png)
 **Get All Tasks**
api:`http://localhost:1000/tasks/`

```json
[
  {
    "_id": "62eaa8fff44996bbc06820ba",
    "title": "this is title",
    "description": "this is the description",
    "createdAt": "2022-08-03T16:57:35.029Z",
    "updatedAt": "2022-08-03T16:57:35.029Z",
    "__v": 0
  },
  {
    "_id": "62eaa90af44996bbc06820bc",
    "title": "this is title",
    "description": "this is the description",
    "createdAt": "2022-08-03T16:57:46.439Z",
    "updatedAt": "2022-08-03T16:57:46.439Z",
    "__v": 0
  },
  {
    "_id": "62eaa90bf44996bbc06820c0",
    "title": "this is title",
    "description": "this is the description",
    "createdAt": "2022-08-03T16:57:47.883Z",
    "updatedAt": "2022-08-03T16:57:47.883Z",
    "__v": 0
  },
  {
    "_id": "62eaa90cf44996bbc06820c2",
    "title": "this is title",
    "description": "this is the description",
    "createdAt": "2022-08-03T16:57:48.794Z",
    "updatedAt": "2022-08-03T16:57:48.794Z",
    "__v": 0
  },
  {
    "_id": "62eab011dc6db60de9a247c0",
    "title": "this is title",
    "description": "this is the description",
    "createdAt": "2022-08-03T17:27:45.876Z",
    "updatedAt": "2022-08-03T17:27:45.876Z",
    "__v": 0
  }
]

```

**Get Single Task**
api:`http://localhost:1000/tasks/62eaa8fff44996bbc06820ba`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x15nigeaat6qgk5exj4w.png)
**Update Task** 
api:`http://localhost:1000/tasks/update/62eaa8fff44996bbc06820ba`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f3y7urgri8y1w97aa5vi.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwl7pax6h5rlvbufqrhk.png)
**Delete Tasks**
api:`http://localhost:1000/tasks/delete/62eaa8fff44996bbc06820ba`


Full Code:
app.js

```javascript
require('dotenv').config()

const express = require('express')
const mongoose = require('mongoose')
const http = require('http')

const setMiddlewares = require('./middleware/middlewares')
const setRoutes = require('./router/routes')



const app = express()
const server = http.createServer(app)

setMiddlewares(app)
setRoutes(app)


const PORT = process.env.PORT || 1000
const DB_USER = process.env.DB_USER
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_NAME = process.env.DB_NAME



mongoose.connect(`mongodb+srv://DB_USER :DB_PASSWORD @mycluster.oazue.mongodb.net/DB_NAME ?retryWrites=true&w=majority`, { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => {
        console.log('Database connect success')
        server.listen(PORT, () => {
            console.log('Server Is Ready')
        })
    }).catch(e => {
        return console.log(e)
    })
```
middleware.js

```javascript
const express = require('express')
const morgan = require('morgan')


const middlewares = [
    morgan('dev'),
    express.urlencoded({ extended: true }),
    express.json()
]

module.exports = (app) => {
    middlewares.forEach(m => {
        app.use(m)
    })
}
```
Task.js

```javascript
const { Schema, model } = require('mongoose')

const taskSchema = new Schema({
    title: {
        type: String
    },
    description: {
        type: String
    }
}, {
    timestamps: true
})

const Task = model('Task', taskSchema)
module.exports = Task
```
crudController.js

```javascript
const Task = require('../model/Task')


// get all tasks
exports.getTaskController = async(req, res, next) => {
    try {
        await Task.find()
            .then(tasks => {
                return res.send(tasks)
            }).catch(err => {
                res.status(500).send({
                    msg: 'no tast found'
                })
            })

    } catch (e) {
        console.log(e)
        next()
    }
}

// get single task

exports.getSingleTask = async(req, res, next) => {
    try {
        let taskId = req.params.id
        await Task.findById(taskId)
            .then(task => {
                if (task) {
                    return res.send(task)
                } else {
                    return res.send({ msg: 'no task found' })
                }

            }).catch(err => {
                res.status(500).send({
                    msg: 'no task found'
                })
            })

    } catch (e) {
        console.log(e)
        next()
    }
}


// create task
exports.createTaskController = async(req, res, next) => {
    try {
        let { title, description } = req.body
        let newTask = new Task({
            title,
            description
        })

        await newTask.save(newTask)
            .then(data => {
                return res.send(data)
            }).catch(err => {
                res.status(500).send({
                    msg: 'task create fail'
                })
            })

        console.log(newTask)
    } catch (e) {
        console.log(e)
        next()
    }
}

//update task 

exports.updateTastController = async(req, res, next) => {
    try {
        let taskId = req.params.id
        let { title, description } = req.body
        let task = await Task.findById(taskId)
        console.log(task)
        if (!task) {
            return res.json({ msg: 'no task found' })
        }

        await Task.findByIdAndUpdate(taskId, {
            $set: {
                title,
                description
            }
        }, { new: true }).then(data => {
            return res.send(data)
        }).catch(err => {
            res.status(500).send({
                msg: 'task update fail'
            })
        })

    } catch (e) {
        console.log(e)
        next()
    }
}

// delete task 

exports.deleteTaskController = async(req, res, next) => {
    try {
        let taskId = req.params.id
        await Task.findByIdAndDelete(taskId)
            .then(data => {
                return res.send(data)
            }).catch(err => {
                res.status(500).send({
                    msg: 'task delete fail'
                })
            })
    } catch (e) {
        console.log(e)
    }
}
```

routes.js

```javascript
const crudRoute = require('../router/cruduRoute')

const routes = [{
        path: '/tasks',
        handler: crudRoute
    },
    {
        path: '/',
        handler: (req, res, next) => {
            return res.json({ msg: 'Welcome to my application' })
        }
    }
]

module.exports = (app) => {
    routes.forEach(r => {
        if (r.path == '/') {
            app.get(r.path, r.handler);
        } else {
            app.use(r.path, r.handler)
        }
    })
}
```

crudRoute.js

```javascript
const router = require('express').Router()
const { getTaskController, getSingleTask, createTaskController, updateTastController, deleteTaskController } = require('../controller/crudController')


router.get('/', getTaskController)
router.get('/:id', getSingleTask)
router.post('/create', createTaskController)
router.put('/update/:id', updateTastController)
router.delete('/delete/:id', deleteTaskController)

module.exports = router
```
package.json

```json
{
    "name": "backend",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "nodemon app.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "dotenv": "^16.0.1",
        "express": "^4.18.1",
        "express-validator": "^6.14.2",
        "moment": "^2.29.4",
        "mongoose": "^6.5.0",
        "morgan": "^1.10.0",
        "multer": "^1.4.5-lts.1",
        "nodemon": "^2.0.19"
    }
}
```
**Files**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d5rrwcvkyr5g1ntkub6.png)