-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (51 loc) · 2.28 KB
/
index.js
File metadata and controls
60 lines (51 loc) · 2.28 KB
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
const express = require("express");
const path = require("path");
const { app } = require("./config/app.js");
// Custom error handling
// const apiErrorHandler = require("./error/apiErrorHandler");
// Middleware to parse incoming JSON requests
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files from specific directories
app.use("/assets", express.static(path.join(__dirname, "public/assets")));
app.use("/composables", express.static(path.join(__dirname, "public/composables")));
app.use("/components", express.static(path.join(__dirname, "public/components")));
app.use(express.static(path.join(__dirname, "public"))); // Serve other static assets
// Middleware to ensure proper MIME types for .js files
app.use((req, res, next) => {
if (req.path.endsWith(".js")) {
res.setHeader("Content-Type", "application/javascript");
}
if (req.path.endsWith(".svg")) {
res.setHeader("Content-Type", "image/svg+xml");
}
if (req.path.endsWith(".json")) {
res.setHeader("Content-Type", "application/json");
}
next();
});
// API routes (ensure these are defined first)
// We are only assuming endpoints stating with /api/
app.use("/api/configs", require("./routes/configs")); // Configuration values
app.use("/api/files", require("./routes/files")); // Add and retrieve files to process.env.DATA
app.use("/api/healthcheck", require("./routes/healthcheck")); // Healthcheck route
app.use("/api/models", require("./routes/models")); // Server side defined models
app.use("/api/library", require("./routes/library")); // Interact with library artifacts
app.use("/api/transcription", require("./routes/transcription")); // Interact with library artifacts
app.use("/api/textToSpeech", require("./routes/textToSpeech")); // Interact with library artifacts
// SPA Fallback: Serve `index.html` for any non-API routes
app.get("*", (req, res) => {
if (req.path.startsWith("/api")) {
res.status(404).send({ error: "API route not found" });
} else {
res.sendFile(path.join(__dirname, "public", "index.html"));
}
});
// 404 error handler for unrecognized routes
app.use((req, res, next) => {
const error = new Error("This site was not found. Perhaps you want to call login?");
error.status = 404;
next(error);
});
// API error handler
// app.use(apiErrorHandler);