-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (75 loc) · 1.93 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
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const firebase = require("firebase");
const port = process.env.PORT || 8000;
const app = express();
var firebaseConfig = {
apiKey: "AIzaSyAcSmLC_s0hwYIly5CmgIEKS4LQhTIC7vI",
authDomain: "bookly-38d82.firebaseapp.com",
databaseURL: "https://bookly-38d82-default-rtdb.firebaseio.com",
projectId: "bookly-38d82",
storageBucket: "bookly-38d82.appspot.com",
messagingSenderId: "1011253680302",
appId: "1:1011253680302:web:73f7677cc51a7f6b02f53f",
};
firebase.initializeApp(firebaseConfig);
//set ejs as a main engine
app.set("view engine", "ejs");
//for using body parser
app.use(bodyParser.urlencoded({ extended: true }));
//join the public folder
app.use(express.static("public"));
let library = [];
let Name;
firebase
.database()
.ref("Books/")
.once("value", (snap) => {
snap.forEach((e) => {
let bookInfo = {};
bookInfo.Name = e.val().Name;
bookInfo.URL = e.val().URL;
bookInfo.Editor = e.val().Editor;
bookInfo.Author = e.val().Author;
bookInfo.Rating = e.val().Rating;
library.push(bookInfo);
});
});
app.get("/", (req, res) => {
res.render("home", { library: library });
});
app.get("/add", (req, res) => {
res.render("add");
});
app.post("/add", (req, res) => {
let Info = req.body;
let URL = Info.BookURL;
Name = Info.BookTitle;
let Author = Info.BookAuthor;
let Rating = Info.BookRating;
let Editor = Info.BookEditor;
let BookInput = {
Name: Name,
URL: URL,
Author: Author,
Rating: Rating,
Editor: Editor,
};
firebase
.database()
.ref("Books/" + Name)
.set({
Name: Name,
URL: URL,
Author: Author,
Rating: Rating,
Editor: Editor,
});
library.push(BookInput);
res.redirect("/");
});
// listen on port 3000
app.listen(port, function () {
console.log("Server started on port ${port}");
});