CRUD APP WITH SORT IN NODE EXPRESS AND MONGODB

 CRUD APP WITH SORT IN NODE EXPRESS AND MONGODB

wriitten in ecma script6 

Dependencies -

 


 File structure  -


Server.js file - 


//requirements 

const express = require('express');

const mongoose = require('mongoose');

const bodyparser = require('body-parser')

const app = express();

 

// database connection

mongoose.connect('mongodb://localhost:27017/testone',{ useNewUrlParser: true ,useUnifiedTopology: true})

.then(()=>console.log("databse is connected"))

.catch((err)=>console.log(err))

 

 

// database schema

var itemSchema = new mongoose.Schema({

    name : String,

    surname :String,

    event : String

})

 

// database collection creation

var item = new mongoose.model('item',itemSchema)

 

// setting view engine to ejs

app.set('view engine','ejs')

 

// using body parser because of  json

app.use(bodyparser.urlencoded({extended:true}))

 

 

// getting request from the client 

app.get('/',(req,res)=>{

    res.render('index');

})

 

// getting add item request

app.get('/additem',(req,res)=>{

    

    item.find({},(err,items)=>{

        if(err)

        {

            console.log("item is not display")

        }

        else{

            res.render('additem',{items:items})

        }

    })

})

 

// edit request

app.get('/edit/:id',(req,res)=>{

    item.findById(req.params.id,(err,items)=>{

        if(err)

        {

            console.log("not found")

        }

        else{

            res.render('edit',{items:items})

        }

    })

}) 

 

 

 

 

// editing the post request by the client

app.post('/edit/:id',(req,res)=>{

 

    item.findByIdAndUpdate(req.params.id,req.body,(err)=>{

        if(err){

            res.redirect('edit/' + req.params.id);

        }

        else {

            console.log('item saved');

            res.redirect('/additem');

        }

    })

})

 

 

// sorting on the basis of client

app.get('/sort',(req,res)=>{

    res.render('sort')

})

 

// finding data provide  by the client  

app.post('/sorted',(req,res)=>{

    item.find({event: req.body.find},(err,items)=>{

        if(err)

        {

            console.log(err)

        }

        else{

            res.render('sorted',{items:items})

        }

    })

})

 

 

 

// sorting on the basis of the list

app.get('/sortedone/:id',(req,res)=>{

    console.log(req.params.id)

    item.findById(req.params.id,(err,items)=>{

        if(err)

        {

            console.log("not found")

        }

        else{

            console.log(items.event)

            item.find({event: items.event},(err,items)=>{

                if(err)

                {

                    console.log(err)

                }

                else{

                    res.render('sortedone',{items:items})

                }

            })

        }

    })

}) 

 

 

// deleting data 

app.get('/delete:id',(req,res)=>{

    item.findByIdAndRemove(req.params.id,(err)=>{

        if(err){

            console.log(err)

        }

        else {

            res.redirect('/additem')

        }

    })

})

 

 

// saving data 

app.post('/additem',(req,res)=>{

    var itemdata = new item(req.body)

   itemdata.save().then(result=>{

        res.redirect('/additem');

        console.log("item saved");

    }).catch((err)=>{

        res.send("error saving data")

    })

})

 

 

// port 

app.listen('3000',(req,res)=>{

    console.log("server started");

})

 



Index.ejs file - 


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

 

<body>

    <h1>welcome to my page</h1>

    <a href="/additem">add item</a>

    

   

</body>

</html>


Additem.ejs file - 


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>hello</title>

</head>

<style>

    table {

      font-family: arial, sans-serif;

      border-collapse: collapse;

      width: 100%;

    }

    

    td, th {

      border: 1px solid #dddddd;

      text-align: left;

      padding: 8px;

    }

    

    tr:nth-child(even) {

      background-color: #dddddd;

    }

    </style>

<body>

    <h3>this is a place where you add item</h3>

    <h2>sort on a basis of event</h2>

    <form action="/sorted" method="POST">

        <input type="text" name="find" placeholder="event name ">

      

        <button>search</a></button>

        </form>

 

        <h3>add items </h3>

    <form action="/additem" method="post">

    <input type="text" name="name" placeholder="name">

    <input type="text" name="surname" placeholder="surname">

    <input type="text" id="myText" name="event" placeholder="rangoli"  value="rangoli" readonly>

    <!-- <input type="text" id="myText" name="event" placeholder="rangoli"  value="rangoli" > -->

    <button type="submit">submit</button>

    </form>

    <br>

    <h3>view items</h3>

    <table>

        <tr>

            <th>no.</th>

            <th>name</th>

            <th>surname</th>

            <th>event</th>

            <th>action</th>

            <th>action</th>

            <th>action</th>

          </tr>

          <% for(let i = 0; i < items.length ; i++ ){ %>

            <tr>

                

              <td> <%= i %></td>

              <td> <%= items[i].name %></td>

              <td> <%= items[i].surname %></td>

              <td> <%= items[i].event %></td>

              <td>  <a href="/edit/<%= items[i]._id %>">edit</a></td>

              <td>  <a href="/delete<%=items[i]._id  %>">delete</a></td>

              <td>  <a href="/sortedone/<%=items[i]._id  %>">see participants</a></td>

            </tr>

 

        <% } %>

    </table>

    

 

 

    

</body>

 

</html>


edit .ejs file - 


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <h1>this is this</h1>

 

    <form  method="post">

        <input type="text" name="name" placeholder="name" value="<%= items.name %>">

        <input type="text" name="surname" placeholder="surname" value="<%= items.suname %>">

        <input type="text" id="myText" name="event" placeholder="rangoli" defaultvalue="rangoli" value="<%= items.event %>">

        <button type="submit">submit</button>

</body>

</html>



Sort.ejs file - 


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>sort</title>

</head>

<style>

    table {

      font-family: arial, sans-serif;

      border-collapse: collapse;

      width: 100%;

    }

    

    td, th {

      border: 1px solid #dddddd;

      text-align: left;

      padding: 8px;

    }

    

    tr:nth-child(even) {

      background-color: #dddddd;

    }

    </style>

<body>

    <h1>this is a sorting page</h1>

 

    <form action="/sorted" method="POST">

    <input type="text" name="find" placeholder="event name ">

    <button type="submit">submit</button>

    </form>

</body>

</html>


Sorted.ejs file - 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>sort</title>

</head>

<style>

    table {

      font-family: arial, sans-serif;

      border-collapse: collapse;

      width: 100%;

    }

    

    td, th {

      border: 1px solid #dddddd;

      text-align: left;

      padding: 8px;

    }

    

    tr:nth-child(even) {

      background-color: #dddddd;

    }

    </style>

<body>

    <h1>this is a sorting page</h1>

 

   

    <table>

        <tr>

            <th>name</th>

            <th>surname</th>

            <th>event</th>

          </tr>

          <% for(let i = 0; i < items.length ; i++ ){ %>

            <tr>

              <td> <%= items[i].name %></td>

              <td> <%= items[i].surname %></td>

              <td> <%= items[i].event %></td>

    

            </tr>

 

        <% } %>

    </table>

</body>

</html>


Sortedone.ejs file -


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>sort</title>

</head>

<style>

    table {

      font-family: arial, sans-serif;

      border-collapse: collapse;

      width: 100%;

    }

    

    td, th {

      border: 1px solid #dddddd;

      text-align: left;

      padding: 8px;

    }

    

    tr:nth-child(even) {

      background-color: #dddddd;

    }

    </style>

<body>

    <h1>this is a sorted one page</h1>

 

   

    <table>

        <tr>

            <th>name</th>

            <th>surname</th>

            <th>event</th>

          </tr>

          <% for(let i = 0; i < items.length ; i++ ){ %>

            <tr>

              <td> <%= items[i].name %></td>

              <td> <%= items[i].surname %></td>

              <td> <%= items[i].event %></td>

    

            </tr>

 

        <% } %>

    </table>

</body>

</html>

 




 



Comments

All time Best Post

Write a program that declares a class named Person. It should have instance variables to record name, age and salary. Use new operator to create a Person object. Set and display its instance variables.

IMAGE CRUD OPERATION USING EXPRESS MONGOOSE MULTER

connect websocket to binance api and store t he true value in the db

express session login system using node express mongodb

how to delete image from the folder directly from a Request (MULTER)

calculate rsi

java program for 1 to 10 print using while loop

How to fetch last one minute volume of BTC in CCXT Nodejs.