-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
69 lines (61 loc) · 1.76 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
require("dotenv").config({ path: "./config.env" });
const cool = require("cool-ascii-faces");
const express = require("express");
const expressLayouts = require("express-ejs-layouts");
const fileUpload = require("express-fileupload");
const session = require("express-session");
const cookieParser = require("cookie-parser");
const flash = require("connect-flash");
const { Pool } = require("pg");
const pool = new Pool({
connectionString: process.env.DATABASE.replace(
"<password>",
process.env.PASSWORD
),
ssl: {
rejectUnauthorized: false,
},
});
const app = express();
const port = process.env.PORT || 3000;
showTimes = () => {
let result = "";
const times = process.env.TIMES || 5;
for (i = 0; i < times; i++) {
result += i + " ";
}
return result;
};
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(expressLayouts);
app.use(cookieParser("CookingBlogSecure"));
app.use(
session({
secret: "CookingBlogSecretSession",
saveUninitialized: true,
resave: true,
})
);
app.use(flash());
app.use(fileUpload());
app.set("layout", "./layouts/main");
app.set("view engine", "ejs");
const routes = require("./server/routes/recipeRoutes.js");
app
.use("/", routes)
.get("/cool", (req, res) => res.send(cool()))
.get("/times", (req, res) => res.send(showTimes()))
.get("/db", async (req, res) => {
try {
const client = await pool.connect();
const result = await client.query("SELECT * FROM test_table");
const results = { results: result ? result.rows : null };
res.render("pages/db", results);
client.release();
} catch (err) {
console.error(err);
res.send("Error " + err);
}
});
app.listen(port, () => console.log(` ${port} tinglanmoqda !!!`));