o134's picture
Upload artifacts/api-server/src/app.ts with huggingface_hub
3e7eccc verified
Raw
History Blame Contribute Delete
1.57 kB
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;