-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (79 loc) · 2.38 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
90
91
92
93
94
95
96
97
98
99
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const dbURI = process.env.dbURI;
const Book = require('./models/book');
const app = express();
const bookRoutes = require('./routes/bookRoutes')
mongoose.connect(dbURI)
.then((result)=>{
app.listen(4000, ()=> {
console.log('app listening at port 4000');
});
})
.catch((err)=> {
console.log(err);
})
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true}));
//mongoose and mongo sandbox routes
app.get('/add-book', (req, res)=> {
const book = new Book({
title: 'What the twilight says',
author: 'Derek Walcott',
genre: 'Non-Fiction',
year: 2025,
description: "This collection forms a volume of remarkable elegance, concision, and brilliance. It includes Walcott's moving and insightful examinations of the paradoxes of Caribbean culture, his Nobel lecture, and his reckoning of the work and significance of such poets as Robert Lowell, Joseph Brodsky, Robert Frost, Les Murray, and Ted Hughes, and of prose writers such as V. S. Naipaul and Patrick Chamoiseau."
});
book.save()
.then((result)=> {
res.send(result);
})
.catch((err)=> {
console.log(err)
})
})
app.get('/all-books', (req, res)=>{
Book.find()
.then((result)=>{
res.send(result);
})
.catch((err)=>{
console.log(err)
})
})
app.get('/single-book', (req, res)=>{
Book.findById('671595be3ab3d076a2fa586c')
.then((result)=>{
res.send(result);
})
.catch((err)=>{
console.log(err)
})
})
app.get('/single-book-pages', (req, res)=>{
Book.findById('671595be3ab3d076a2fa586c')
.then((result)=>{
res.send(result);
})
.catch((err)=>{
console.log(err)
})
})
app.get('/single', (req, res)=> {
res.render('single', { title: 'book detail'});
})
app.get('/', (req, res)=>{
res.redirect('/books');
});
app.use(bookRoutes);
app.get('/about', (req, res)=> {
res.render('about', { title: 'About Library App'})
});
app.get('/about-us', (req, res)=> {
res.redirect('/about')
});
app.use((req, res)=>{
res.status(404).render('404', { title: 'Page not found'});
});