-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
30 lines (25 loc) · 854 Bytes
/
server.js
File metadata and controls
30 lines (25 loc) · 854 Bytes
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
const express = require("express");
const fs = require("fs");
const path = require("path");
const { compile } = require("./lib");
const port = process.env.PORT || 3000;
const app = express();
app.use(express.raw({ type: "*/*" }));
app.get("/", (req, res) => {
res.type("html").send(fs.readFileSync(path.join(__dirname, "index.html")));
});
app.post("/compile", async (req, res) => {
try {
const tmpDir = fs.mkdtempSync(process.env.TMPDIR);
const inFilename = path.join(tmpDir, "invoice.yaml");
const outFilename = path.join(tmpDir, "invoice.pdf");
fs.writeFileSync(inFilename, req.body);
await compile(inFilename, outFilename);
res.download(outFilename);
} catch (err) {
console.error(err);
res.status(500).send(`${err}\n${err.stack}`);
}
});
app.listen(port, "0.0.0.0");
console.log(`Listening to ${port}`);