-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
117 lines (97 loc) · 2.65 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const express = require('express')
var exphbs = require('express-handlebars');
const app = express()
const bodyParser = require("body-parser")
const cors = require("cors")
const mongoose = require("mongoose")
const bookRouter = require("./routes/book_routes")
const api_helper = require('./API_helper')
const Book = require("./models/book.js")
const path = require ("path")
// import {v4 as uuidv4} from 'uuid'
const port = 3000
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('views/images'));
app.use(express.static(path.join(__dirname, 'stylesheets')))
const dbConn = process.env.MONGODB_URI || "mongodb://localhost/libro_app"
// when we need to deploy:
// if(process.env.NODE_ENV !== "production"){
// require("dotenv").config();
// }
mongoose.connect(
dbConn,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
},
err => {
if (err) {
console.log("Error connecting to database", err)
} else {
console.log("Connected to database!")
}
}
)
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
app.get('/', (req, res) => {
res.render("home");
})
app.get("/search", (req, res)=>{
res.render("search")
})
app.get("/register", (req,res)=> {
res.render("register")
})
app.post("/books/new", (req, res) => {
var addedBook = {
title: req.body.title,
author: req.body.author,
category: req.body.category,
published_year: req.body.published_year
}
new Book(addedBook).save()
console.log(addedBook)
res.redirect("/books")
// this needs to render handlebars page??
})
// app.get("/books", (req, res)=> {
// res.render("books")
// })
app.get("/login", (req, res)=>{
res.render("login")
})
app.get("/books/addBook", (req, res)=>{
res.render("addBook")
})
app.get("/books", async function(req,res) {
let books = await Book.find({}).lean()
console.log(books)
res.render("books", {books})
})
// app.get('/books', function(req, res) {
// // var collection = req.db.get('quotes');
// Book.find({}, {}, function(err, book) {
// var obj = {
// fromDB: book,
// title: "This is a title of the page"
// }
// res.render('books', obj);
// });
// });
// app.get('/getAPIResponse', (req, res) => {
// api_helper.make_API_call('https://jsonplaceholder.typicode.com/todos/1')
// .then(response => {
// res.json(response)
// })
// .catch(error => {
// res.send(error)
// })
// })
app.use("/books", bookRouter)
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})