-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (49 loc) · 1.51 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
// imports
const express = require("express");
const app = express();
const viewsRouter = require("./routes/views.js");
const apiRouter = require("./routes/api.js");
const { PORT, MOTD, fileupload_config } = require("./config.js");
const exphbs = require("express-handlebars");
const crypto = require("crypto");
const fs = require("fs");
const cookieParser = require("cookie-parser");
const fileUpload = require("express-fileupload");
// secret key generation
function generateHash() {
//crypto secretkey
const secretKey = crypto.randomBytes(256).toString("base64");
const hash = crypto.createHmac("sha256", secretKey).digest("hex");
return `SECRET_KEY="${hash}"`;
}
fs.writeFileSync(".ENV", generateHash().toString());
// cookie-parser middleware
app.use(cookieParser());
// fupload middleware
app.use(fileUpload(fileupload_config));
// body-parser
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// handlebars
app.set("view engine", "hbs");
app.engine(
"hbs",
exphbs.engine({
defaultLayout: "main",
layoutsDir: __dirname + "/views/layouts",
extname: "hbs",
})
);
// static middlewares
app.use(express.static(__dirname + "/public"));
app.use(
"/bootstrap",
express.static(__dirname + "/node_modules/bootstrap/dist")
);
app.use("/uploads", express.static(__dirname + "/uploads"));
app.use("/axios", express.static(__dirname + "/node_modules/axios/dist"));
// routes
app.use("/api/v1/", apiRouter);
app.use("/", viewsRouter);
// server launch
app.listen(PORT, () => console.log(MOTD));