Setup Node On System
- If you don't have node installed on your system then you can download it from here https://nodejs.org/en/.
- After installing the node on system , open cmd and type node-v.
- It will give node version like this,
Setup MongoDB On System
Setup Our Node Project
- Create a new folder nodecurd
- Change to the folder to nodecurd
- Type npm init to setup node project.
- A package.json file will automatically get added in the project.
- Now type npm install for adding node packages.
- npm install express mongoose body-parser ejs
A little info about the packages,
- express - It is a framework and all the applications will be built on it.
- body-parser - This will fetch FormData from the request.
- mongoose - this will be used for setting connections with a database.
- ejs - This is a view engine. The view engine is responsible for creating HTML from your views.
Make 2 folders
*as mentioned above the views will contain .ejs files.which will contain HTML and databindings using ejs.
Models
- The models will contain the collections(table) schema.
- Here we will make a collection named empdata having field name
- const mongoose = require('mongoose');
-
- const curdSchema= new mongoose.Schema({
- name: String
- })
-
- module.exports=mongoose.model('empdata',curdSchema)
App.js
- const express= require('express');
- const mongoose = require('mongoose');
- const bodyParser = require('body-parser');
- const curd = require('./models/curd');
-
-
- const app =express();
-
- mongoose.connect('mongodb://localhost:27017/curdDemo')
- .then(()=>console.log('connected')).catch(err=>console.log(err))
-
-
- app.set('view engine','ejs');
-
-
- app.use(bodyParser.urlencoded({extended:false}))
-
-
- app.get('/',(req,res)=>{
- curd.find((err,data)=>{
- if(err){
- console.log(err)
- }
- else if(!data){
- res.render('home',{data:{}})
- }
- else{
- res.render('home',{data:data})
- }
- })
- })
-
-
- app.post('/add',(req,res)=>{
- const curds = new curd({
- name: req.body.uname
- })
- curds.save((err,data)=>{
- if(err){
- console.log(err)
- }
- res.redirect('/')
- })
- })
-
-
-
- app.get('/edit/:id',(req,res)=>{
- curd.find({_id:req.params.id},(err,data)=>{
- if(err){
- console.log(err)
- }
- console.log('edit data',data)
- res.render('edit',{edata:data})
- })
- })
-
-
- app.post('/update',(req,res)=>{
- const upCURD = {
- name : req.body.uname
- }
- curd.update({_id:req.body.id},{$set:upCURD},(err,data)=>{
- if(err){
- console.log('update error',err)
- }
- console.log(data)
- res.redirect('/')
- })
- })
-
-
- app.get('/delete/:id',(req,res)=>{
- curd.deleteOne({_id:req.params.id},(err,data)=>{
- if(err){
- console.log(err)
- }
- res.redirect('/')
- })
- })
-
-
-
- const port = process.env.PORT || 3000 ;
- app.listen(port,()=>console.log(`server running at ${port}`))
-
- module.exports=app;
home.ejs
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Document</title>
- </head>
- <body>
-
- <h2>Add Data</h2>
- <form action="/add" method="POST">
- <label for="unmae">Enter Name</label>
- <input type="text" name="uname" required><br/>
- <button type="submit">save</button>
- </form>
- <br><br>
-
-
- <% if (data.length > 0) {%>
- <table>
- <thead>
- <tr><th>Name</th>
- <th>edit</th>
- <th>delete</th>
- </tr>
- </thead>
- <tbody>
- <% data.forEach(function(data, index) { %>
- <tr>
- <td> <%= data.name %> </td>
-
-
- <td><a href="/edit/<%= data._id %>">edit</a></td>
-
- <td><a href="/delete/<%= data._id %>">delete</a></td>
- </tr>
-
- <% }) %>
- </tbody>
- </table>
- <% } %>
- </body>
- </html>
edit.ejs
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Document</title>
- </head>
- <body>
-
- <h1>Edit Data</h1>
- <form action="/update" method="POST">
- <table>
- <thead>
- <tr><th>Name</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td><input type="text" name="uname" value="<%= edata[0].name %>" > </td>
- <td><input type="hidden" name="id" value="<%= edata[0]._id %>"></td>
- <td><input type="submit" value="submit"></td>
- </tr>
- </tbody>
- </table>
- </form>
-
- </body>
- </html>