-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (98 loc) · 2.83 KB
/
index.js
File metadata and controls
112 lines (98 loc) · 2.83 KB
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
const PORT = 3005; // 서버 포트
const TIMEOUT = 1000 * 60; // PDF 변환 타임아웃 시간
const PDFOPTION = {
// Puppeteer PDF 옵션
path: "resume.pdf",
format: "A4",
printBackground: true,
preferCSSPageSize: true,
};
const VIEWPORT = {
// Puppeteer 뷰포트
width: 842,
height: 1191,
};
const puppeteer = require("puppeteer");
const { config } = require("dotenv");
const express = require("express");
const axios = require("axios");
const cors = require("cors");
const fs = require("fs");
// 서버 기본 설정
config();
const app = express();
app.use(cors());
app.use(express.json({ extended: true }));
app.post("/all", async (req, res) => {
try {
if (!!!req.headers.authorization) {
res.status(403);
res.json({ message: "토큰이 필요합니다" });
return;
}
if (!!!req.body || !!!req.body.grade) {
res.status(404);
res.json({ message: "학년이 필요합니다" });
return;
}
const url = `${process.env.SERVICE_URL}/${req.body.grade}`;
const browser = await puppeteer.launch({
headless: "new",
args: [
`--window-size=${VIEWPORT.width},${VIEWPORT.height}`,
"--disable-dev-shm-usage",
"--no-sandbox",
"--disable-setuid-sandbox",
],
});
const page = await browser.newPage();
page.setViewport(VIEWPORT);
await page.setCookie([
{
name: "access_token",
value: req.headers.authorization.split(" ")[1],
domain: ".localhost",
},
{
name: "role",
value: "teacher",
domain: ".localhost",
},
]);
await page.goto(url, {
waitUntil: ["networkidle0", "load"],
timeout: TIMEOUT,
});
const data = {
index: await page.evaluate(() =>
Array.from(document.querySelectorAll("span.section"))?.map((i) => {
const section = i.innerHTML.split("_");
return {
name: section[1],
student_number: Number(section[2]),
page_number: Number(section[0]),
major: section[3],
};
})
),
};
await page.emulateMediaType("screen");
await page.pdf(PDFOPTION);
await browser.close();
const form = new FormData();
form.append("pdf", new File([fs.readFileSync("./resume.pdf")], "resume.pdf"));
form.append("index", new Blob([JSON.stringify(data)], { type: "application/json" }));
axios
.post(`${process.env.API_URL}/library?grade=${req.body.grade}`, form, {
headers: { authorization: req.headers.authorization },
})
.then(() => {
res.status(200);
res.json({ message: "Successed" });
});
} catch (err) {
res.status(500);
res.json({ message: err.message || "Error occured" });
}
});
app.listen(PORT, () => console.log(`서버가 ${PORT}에서 가동되고 있습니다`));