-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (49 loc) · 1.29 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
49
50
51
52
53
const express = require("express");
const cors = require("cors");
const { bundleMDX } = require("mdx-bundler");
const fs = require("fs");
const path = require("path");
require("dotenv").config();
const port = process.env.PORT || 3000;
const client = process.env.CLIENT || 5173;
const app = express();
app.use(
cors({
origin: client,
methods: ["GET"],
credentials: true,
})
);
app.get("/api/mdx-server/:project", async (req, res) => {
try {
const filename = req.params.project;
const filepath = path.join(__dirname, "/mdx", `${filename}.mdx`);
const mdx = fs.readFileSync(filepath, "utf-8");
const { code } = await bundleMDX({
source: mdx,
globals: {
"@react-three/fiber": {
varName: "reactThreeFiber",
namedExports: ["Canvas", "useFrame", "useLoader"],
defaultExport: false,
},
"@react-three/drei": {
varName: "reactThreeDrei",
namedExports: ["OrbitControls"],
defaultExport: false,
},
three: {
varName: "three",
},
},
});
res.json({ code });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.listen(port, () => {
console.log(
`MDX server is running on port ${port} in ${process.env.NODE_ENV}`
);
});