Skip to content

Commit e5b8587

Browse files
committed
Reroute doc and frontend endpoints using routers
1 parent 3f13898 commit e5b8587

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

server/src/index.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import express from "express";
33
import typesRouter from "./routers/types.js";
44
import activitiesRouter from "./routers/activities.js";
55
import docsRouter from "./routers/docs.js";
6+
import frontendRouter from "./routers/frontend.js";
7+
68
import cors from "cors";
7-
import path from "path";
89
import mongoose from "mongoose";
910

1011
dotenv.config();
11-
const baseDirectory = path.resolve();
1212

1313
try {
1414
await connectToDatabase();
1515
const port = getPort();
1616

1717
const app = express();
1818
setupAppMiddleware(app);
19-
setAppRoutes(app);
19+
setupAppRoutes(app);
2020

2121
app.listen(port, () =>
2222
console.log(`Listening on http://localhost:${port}`)
@@ -29,7 +29,6 @@ try {
2929
function setupAppMiddleware(app) {
3030
app.use(cors());
3131
app.use(express.json());
32-
app.use(express.static(getClientBuildPath()));
3332
return app;
3433
}
3534

@@ -40,15 +39,11 @@ async function connectToDatabase() {
4039
});
4140
}
4241

43-
function setAppRoutes(app) {
42+
function setupAppRoutes(app) {
43+
app.use("/", frontendRouter);
4444
app.use("/types", typesRouter);
4545
app.use("/activities", activitiesRouter);
4646
app.use("/docs", docsRouter);
47-
48-
// Setup React routing
49-
app.get("/*", (req, res) => {
50-
res.sendFile(path.join(getClientBuildPath(), "index.html"));
51-
});
5247
}
5348

5449
function getPort() {
@@ -61,16 +56,6 @@ function getPort() {
6156
return port;
6257
}
6358

64-
function getClientBuildPath() {
65-
const clientBuildPath = path.join(baseDirectory, "frontend/build");
66-
if (!clientBuildPath) {
67-
throw new Error(
68-
`Could not resolve client build path.\tBase directory:${baseDirectory}`
69-
);
70-
}
71-
return clientBuildPath;
72-
}
73-
7459
function getMongoConnection() {
7560
const connection = process.env.MONGODB_URI;
7661
if (!connection) {

server/src/routers/docs.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
11
import express from "express";
2+
import path from "path";
23
import swaggerUi from "swagger-ui-express";
34
import { swaggerSpec } from "../../swagger.config.js";
45

6+
const baseDirectory = path.resolve();
7+
58
const router = express.Router();
69

7-
router.use("/", swaggerUi.serve);
8-
router.get("/", swaggerUi.setup(swaggerSpec));
10+
router.use("/api", swaggerUi.serve);
11+
router.get("/api", swaggerUi.setup(swaggerSpec));
12+
13+
// Server jsDocs website
14+
router.use("/server", express.static(getServerDocumentationPath()));
15+
router.get("/server", (req, res) => {
16+
res.sendFile(path.join(getServerDocumentationPath(), "index.html"));
17+
});
18+
19+
// Frontend jsDocs website
20+
router.use("/frontend", express.static(getFrontendDocumentationPath()));
21+
router.get("/frontend", (req, res) => {
22+
res.sendFile(path.join(getFrontendDocumentationPath(), "index.html"));
23+
});
24+
25+
function getServerDocumentationPath() {
26+
const docPath = path.join(baseDirectory, "server/documentation/jsdocs");
27+
if (!docPath) {
28+
throw new Error(
29+
`Could not resolve server documentation build path. Ensure that you run \"npm run generate-docs\" in the base directory.`
30+
);
31+
}
32+
return docPath;
33+
}
34+
35+
function getFrontendDocumentationPath() {
36+
const docPath = path.join(baseDirectory, "frontend/documentation/jsdocs");
37+
if (!docPath) {
38+
throw new Error(
39+
`Could not resolve server documentation build path. Ensure that you run \"npm run generate-docs\" in the base directory.`
40+
);
41+
}
42+
return docPath;
43+
}
944

1045
export default router;

server/src/routers/frontend.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import express from "express";
2+
import path from "path";
3+
4+
const baseDirectory = path.resolve();
5+
6+
const router = express.Router();
7+
8+
router.use("/", express.static(getClientBuildPath()));
9+
router.get("/", (req, res) => {
10+
res.sendFile(path.join(getClientBuildPath(), "index.html"));
11+
});
12+
13+
function getClientBuildPath() {
14+
const clientBuildPath = path.join(baseDirectory, "frontend/build");
15+
if (!clientBuildPath) {
16+
throw new Error(
17+
`Could not resolve client build path.\tBase directory:${baseDirectory}`
18+
);
19+
}
20+
return clientBuildPath;
21+
}
22+
23+
export default router;

0 commit comments

Comments
 (0)