├── db.js ├── models └── book.model.js ├── app-structure.txt ├── package.json ├── README.md ├── views ├── layouts │ └── mainLayout.hbs └── books │ ├── index.hbs │ └── addOrEdit.hbs ├── server.js └── controllers └── book.controller.js /db.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const dbUri = 'your-mongo-db-url' 4 | 5 | 6 | module.exports = () => mongoose.connect(dbUri) -------------------------------------------------------------------------------- /models/book.model.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | 4 | module.exports = mongoose.model('Book', { 5 | title: String, 6 | author: String, 7 | publishedYear: Number, 8 | price: Number, 9 | }) -------------------------------------------------------------------------------- /app-structure.txt: -------------------------------------------------------------------------------- 1 | ▼ controllers 2 | book.controller.js 3 | ▼ models 4 | book.model.js 5 | ▼ views 6 | ▼ books 7 | addOrEdit.hbs 8 | index.hbs 9 | ▼ layouts 10 | mainLayout.hbs 11 | db.js 12 | server.js -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "book_register", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.20.2", 13 | "express": "^4.19.2", 14 | "express-handlebars": "^7.1.3", 15 | "mongoose": "^8.4.3", 16 | "nodemon": "^3.1.4" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node.js CRUD with MongoDB, Express and Handlebars 2 | 3 | This a demo project from YouTube Tutorial given below 4 | 5 | ## How it works ? 6 | 7 | :tv: Video tutorial on this same topic 8 | Url : https://youtu.be/9VHTDhwo9u0 9 | 10 | Video demonstrating CRUD Operation in Node.js App with MongoDB Express and Handlebars 13 | -------------------------------------------------------------------------------- /views/layouts/mainLayout.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Book Register App. 5 | 7 | 8 | 9 | 10 |
11 |
12 |
13 |
14 |
15 |

16 | Book Register 17 |

18 |
19 |
20 | {{{body}}} 21 |
22 |
23 |
24 |
25 | 26 |
27 |
28 |
29 | -------------------------------------------------------------------------------- /views/books/index.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | {{#each books}} 16 | 17 | 18 | 19 | 20 | 28 | 29 | 30 | {{/each}} 31 | 32 |
TitleAuthorPrice 8 |
9 | + Add 10 |
11 |
{{this.title}}{{this.author}}${{this.price}} 21 |
22 | Edit 23 | 26 |
27 |
-------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const path = require('path') 3 | const { engine } = require('express-handlebars') 4 | const bodyParser = require('body-parser') 5 | 6 | 7 | 8 | //local imports 9 | const connectDb = require('./db') 10 | const bookRoutes = require('./controllers/book.controller') 11 | 12 | const app = express() 13 | app.use(bodyParser.urlencoded({ extended: true })) 14 | app.use(bodyParser.json()) 15 | 16 | 17 | //routing 18 | app.use('/books', bookRoutes) 19 | 20 | //configure view engine 21 | app.set('views', path.join(__dirname, 'views')) 22 | app.engine('.hbs', engine({ 23 | extname: "hbs",//index.hbs 24 | layoutsDir: path.join(__dirname, 'views/layouts'), 25 | defaultLayout: 'mainLayout.hbs' 26 | })) 27 | app.set('view engine', '.hbs') 28 | 29 | 30 | connectDb() 31 | .then(data => { 32 | console.log('db connection succeeded.'); 33 | app.listen(3000, () => { 34 | console.log('server started at 3000.'); 35 | }).on('error', err => 36 | console.log('server ignition failed:\n', err)) 37 | }) 38 | .catch(err => console.log('error in connecting db\n:', err)) -------------------------------------------------------------------------------- /views/books/addOrEdit.hbs: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | 5 | 6 |
7 |
8 | 9 | 10 |
11 |
12 |
13 |
14 | 16 | 17 |
18 |
19 |
20 |
21 | 22 | 23 |
24 |
25 |
26 |
27 | ☰ View All 28 | 31 |
32 |
-------------------------------------------------------------------------------- /controllers/book.controller.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const router = express.Router() 3 | 4 | const Book = require('../models/book.model') 5 | 6 | 7 | router.get('/', (req, res) => { 8 | Book.find().lean() 9 | .then(data => { 10 | res.render("books/index", { books: data }) 11 | }) 12 | .catch(err => 13 | console.log('error during fetching operation:\n', err)) 14 | }) 15 | 16 | 17 | router.get('/addOrEdit', (req, res) => { 18 | res.render('books/addOrEdit') 19 | }) 20 | 21 | router.get('/addOrEdit/:id', (req, res) => { 22 | Book.findById(req.params.id).lean() 23 | .then(data => res.render('books/addOrEdit', { book: data })) 24 | .catch(err => 25 | console.log('error while retrieving the record:\n', err)) 26 | 27 | }) 28 | 29 | router.post('/addOrEdit', (req, res) => { 30 | const book = { 31 | title: req.body.title, 32 | author: req.body.author, 33 | publishedYear: req.body.publishedYear, 34 | price: req.body.price, 35 | } 36 | const { _id } = req.body 37 | if (_id == '') 38 | new Book({ ...book }).save() 39 | .then(data => res.redirect('/books')) 40 | .catch(err => console.log('error during insertion:\n', err)) 41 | else 42 | Book.findByIdAndUpdate(_id, book) 43 | .then(data => res.redirect('/books')) 44 | .catch(err => console.log('error during update operation:\n', err)) 45 | }) 46 | 47 | router.post('/delete/:id', (req, res) => { 48 | Book.findByIdAndDelete(req.params.id) 49 | .then(data => res.redirect('/books')) 50 | .catch(err => console.log('error during deletion:\n', err)) 51 | }) 52 | 53 | module.exports = router --------------------------------------------------------------------------------