-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (75 loc) · 2.82 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var express= require('express');
var app = express();
const path = require('path');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var mongoose = require('mongoose');
var flash = require('express-flash');
var Book = require('./Book.model');
var Port = process.env.PORT || 3000;
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser('secret'));
app.use(session({
cookie: { maxAge: 60000 },
saveUninitialized: true,
resave: 'true',
secret: 'secret'
}));
app.use(flash());
//connect to the databases
mongoose.connect('mongodb://heroku_wx4ph96r:d20cmq78ldc1g9ck1vhl1rhlaq@ds151202.mlab.com:51202/heroku_wx4ph96r',{useNewUrlParser: true,useUnifiedTopology: true});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log("we're connected!");
});
// app index page
app.get('/',function(req,res){
Book.find({})
.exec(function(err,books){
//console.log(books);
res.render('index',{title: 'Node js App','Books':books});
});
});
// Page to enter new Book
app.get('/newbook',function(req,res){
res.render('addbook',{'title':'Add New Book'});
});
// save data into the database
app.post('/newbook/add',function(req,res){
Book.insertMany([{'title':req.body.title,'author':req.body.author,'image':req.body.bookurl,'category':req.body.category}],function(err,result){
console.log("Inserted 1 documents into the collection");
req.flash('success', 'Record Added SuccessFully!');
res.redirect('/');
});
});
// getting the form to edit the existing data
app.get('/book/edit/:id',function(req,res){
Book.findOne({'_id':req.params.id}).exec(function(err,book){
res.render('editbook',{'title':'Edit Book Information','book':book});
});
});
//updating the record in the database
app.post('/book/update',function(req,res){
Book.updateOne({'_id':req.body.id},{$set: {'title':req.body.title,'author':req.body.author,'image':req.body.bookurl,'category':req.body.category}},function(err,result){
req.flash('success', 'Record Updated SuccessFully!');
res.redirect('/');
});
});
// delete the data from the database
app.get('/book/delete/:id',function(req,res){
//console.log(req.params.id);
//res.send(req.params.id);
Book.deleteOne({'_id':req.params.id},function(err,result){
req.flash('success', 'Record Deleted SuccessFully!');
res.redirect('/');
});
});
app.listen(Port,()=>{
console.log(`app listening on port ${Port}`);
});