-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (51 loc) · 1.23 KB
/
main.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
const fs = require("fs");
const HTMLtoDOCX = require("html-to-docx");
const express = require("express");
const cors = require("cors");
const { ROOT_URL } = process.env;
const app = express();
app.use(cors());
app.options("*", cors());
// Body parser middleware for handling POST requests
app.use(express.json({ limit: "999mb" }));
app.use("/static", express.static("public"));
app.get("/", async (req, res) => {
res.status(200).send({
status: "success",
data: {
message: "Please POST html and name",
},
});
});
app.post("/", async (req, res) => {
const { html, name } = req.body;
const body = `
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>${html}</body>
</html>
`;
const buff = await HTMLtoDOCX(body);
fs.writeFile(`./public/${name}.docx`, buff, (err) => {
if (err) {
res.status(500).send({
status: "error",
data: {
message: "An error occurred, please try again.",
},
});
} else {
res.status(200).send({
status: "success",
data: {
url: `${ROOT_URL}/static/${name}.docx`,
},
});
}
});
});
app.listen(3000, () => {
console.log(`Running :3000 port`);
});