-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (40 loc) · 1.23 KB
/
server.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
import React from "react";
import ReactPDF from "@react-pdf/renderer";
import ConsentForm from "./consentform.js";
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// API calls
app.get("/api/hello", (req, res) => {
res.send({ express: "Hello From Express" });
});
app.post("/api/generatepdf", (req, res) => {
const fileName = `${__dirname}/forms/${req.body.name.replace(
/\s+/g,
"-"
)}-consent.pdf`;
console.log(fileName);
console.log(req.body);
ReactPDF.render(
<ConsentForm
name={req.body.name}
date={req.body.date}
signature={req.body.signature}
/>,
`${fileName}`
);
res.send(`PDF file generated at ${fileName}`);
});
if (process.env.NODE_ENV === "production") {
// Serve any static files
app.use(express.static(path.join(__dirname, "client/build")));
// Handle React routing, return all requests to React app
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
app.listen(port, () => console.log(`Listening on port ${port}`));