File size: 1,565 Bytes
1804b24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7281501
 
 
 
 
 
1804b24
 
7281501
3e7eccc
7281501
 
3e7eccc
7281501
cc842b1
 
ed6e301
7281501
 
 
 
 
 
 
1804b24
 
 
 
 
 
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
import express, { type Express } from "express";
import cors from "cors";
import pinoHttp from "pino-http";
import router from "./routes";
import { logger } from "./lib/logger";

const app: Express = express();

app.use(
  pinoHttp({
    logger,
    serializers: {
      req(req) {
        return {
          id: req.id,
          method: req.method,
          url: req.url?.split("?")[0],
        };
      },
      res(res) {
        return {
          statusCode: res.statusCode,
        };
      },
    },
  }),
);
const frontendUrl = process.env["FRONTEND_URL"] || "*";
app.use(cors({
  origin: frontendUrl === "*" ? "*" : frontendUrl.split(","),
  credentials: true
}));

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

app.use("/api", router);

// Serve static files from the React app
const frontendDistPath = path.resolve(__dirname, "../../design-studio/dist/public");
app.use(express.static(frontendDistPath));


// Handle React routing, return all requests to React app
app.get(/.*/, (req, res, next) => {


  if (req.path.startsWith("/api")) {
    return next();
  }
  res.sendFile(path.join(frontendDistPath, "index.html"));
});


app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
  logger.error(err);
  res.status(500).json({ error: err.message || "Internal Server Error", stack: err.stack });
});

export default app;