forked from heroku/node-js-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (131 loc) · 4.06 KB
/
index.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const express = require("express");
const path = require("path");
const PORT = process.env.PORT || 5000;
const bodyParser = require("body-parser");
const cors = require("cors");
const jwt = require("express-jwt");
const uuidv4 = require("uuid/v4");
const { Pool, Client } = require("pg");
const connectionString = process.env.DATABASE_URL;
const JWT_SECRET = process.env.JWT_SECRET;
const pool = new Pool({
connectionString: connectionString
});
app = express()
.use(express.static(path.join(__dirname, "public")))
.use(bodyParser.json())
.use(cors())
.set("views", path.join(__dirname, "views"))
.set("view engine", "ejs")
.get("/", (req, res) => res.render("pages/index"))
.get("/auth/validate_access_token", (req, res, next) => {
if (req.headers && req.headers.access_token === JWT_SECRET) {
res.send(200, { result: "SUCCESS" });
} else {
res.status(400).send({ error: "Missing or Invalid token" });
}
})
.use("/api", (req, res, next) => {
if (req.headers && req.headers.access_token === JWT_SECRET) {
next();
} else {
res.status(400).send({ error: "Missing or Invalid token" });
}
})
.get("/api/get_books", function(request, response) {
pool.connect().then(client => {
return client
.query("SELECT * FROM public.books")
.then(res => {
client.release();
response.setHeader("Content-Type", "application/json");
response.send(JSON.stringify({ result: res.rows }));
})
.catch(e => {
client.release();
console.error(e);
res.status(400).send({ error: "Missing or Invalid token" });
});
});
})
.post("/api/add_book", function(request, response) {
pool.connect().then(client => {
const newUuid = uuidv4();
console.log(request.body);
var book = request.body.book;
return client
.query(
`INSERT INTO public.books(
"bookName", "bookAuthor", "bookYear", "bookPrice", "bookID")
VALUES ($1, $2, $3, $4, $5);`,
[
book.bookName,
book.bookAuthor,
book.bookYear,
book.bookPrice,
book.bookID
]
)
.then(res => {
client.release();
response.setHeader("Content-Type", "application/json");
response.send(JSON.stringify({ result: book.bookID }));
})
.catch(e => {
client.release();
console.error(e);
response.status(400).send({ error: e });
});
});
})
.post("/api/edit_book", function(request, response) {
pool.connect().then(client => {
console.log(request.body);
var book = request.body.book;
return client
.query(
`UPDATE public.books
SET "bookName"=$1, "bookAuthor"=$2, "bookYear"=$3, "bookPrice"=$4
WHERE "bookID" = $5 RETURNING "bookID";`,
[
book.bookName,
book.bookAuthor,
book.bookYear,
book.bookPrice,
book.bookID
]
)
.then(res => {
client.release();
response.setHeader("Content-Type", "application/json");
response.send(JSON.stringify({ result: res.rows }));
})
.catch(e => {
client.release();
console.error(e);
response.status(400).send({ error: e });
});
});
})
.post("/api/delete_book", function(request, response) {
pool.connect().then(client => {
var bookID = request.body.bookID;
return client
.query(
`DELETE FROM public.books
WHERE "bookID" = $1 RETURNING *;`,
[bookID]
)
.then(res => {
client.release();
response.setHeader("Content-Type", "application/json");
response.send(JSON.stringify({ result: res.rows }));
})
.catch(e => {
client.release();
console.error(e);
response.status(400).send({ error: e });
});
});
})
.listen(PORT, () => console.log(`Listening on ${PORT}`));