-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (70 loc) · 2.14 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
let express = require("express");
let router = express.Router();
const mentahanData = require("./../utils/pusat_pengolah_data");
const validator = require("validator");
const spclChar = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~\n\t]/;
let app = express();
/* GET home page. */
router.get("/", function (req, res, next) {
res.render("index", { title: "SHORTCUT-DOCX", hello: "hello world" });
});
router.get("/home", (req, res) => {
res.render("home", {
title: "SHORTCUT-DOCX",
});
});
function calculateDataSize(data) {
const jsonString = JSON.stringify(data);
const byteSize = new TextEncoder().encode(jsonString).length;
console.log(`Size of data: ${byteSize} bytes`);
return byteSize;
}
router.post("/home", async function (req, res) {
let data = req.body.data;
// let dataSize = calculateDataSize(data)
if (validator.isByteLength(data.teks, { min: 70, max: undefined })) {
if (
spclChar.test(data.teks) &&
/[a-zA-Z\d]/.test(data.teks) &&
validator.isJSON(JSON.stringify(data.ref))
) {
try {
let docxBuffer = await mentahanData(data);
res.set({
"Content-Type":
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"Content-Disposition": "attachment; filename=Makalah.docx",
});
return res.send(docxBuffer);
} catch (error) {
console.error(error);
return res.status(500).send("Error processing data");
}
} else {
return res.redirect("/home");
}
} else {
return res.redirect("/home");
}
});
router.get("/referensi", async function (req, res) {
res.render("referensi", {
title: "SHORTCUT-DOCX",
});
});
router.get("/tutorial", async function (req, res) {
res.render("tutorial", {
title: "SHORTCUT-DOCX",
});
});
router.get("/kebijakan-privasi", async function (req, res) {
res.render("kebijakan-privasi", {
title: "SHORTCUT-DOCX",
});
});
router.get("/tentang-kami", async function (req, res) {
res.render("tentang-kami", {
title: "SHORTCUT-DOCX",
});
});
module.exports = router;