mirror of
https://github.com/sakaljurgis/best-choice.git
synced 2026-07-08 21:47:40 +00:00
Serve built client from server
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import fs from 'node:fs';
|
||||||
|
import path from 'node:path';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import apiRouter from './routes/api-router.js';
|
import apiRouter from './routes/api-router.js';
|
||||||
import { env } from './config/env.js';
|
import { env } from './config/env.js';
|
||||||
@@ -7,6 +9,39 @@ const app = express();
|
|||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(env.apiBasePath, apiRouter);
|
app.use(env.apiBasePath, apiRouter);
|
||||||
|
|
||||||
|
if (env.client.serve) {
|
||||||
|
const clientDistPath = env.client.distPath;
|
||||||
|
const indexHtmlPath = path.join(clientDistPath, 'index.html');
|
||||||
|
|
||||||
|
if (fs.existsSync(clientDistPath) && fs.existsSync(indexHtmlPath)) {
|
||||||
|
app.use(express.static(clientDistPath));
|
||||||
|
app.get('*', (req, res, next) => {
|
||||||
|
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
const basePath = env.apiBasePath.endsWith('/')
|
||||||
|
? env.apiBasePath
|
||||||
|
: `${env.apiBasePath}/`;
|
||||||
|
|
||||||
|
if (
|
||||||
|
env.apiBasePath &&
|
||||||
|
env.apiBasePath !== '/' &&
|
||||||
|
(req.path === env.apiBasePath || req.path.startsWith(basePath))
|
||||||
|
) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.sendFile(indexHtmlPath);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.warn(
|
||||||
|
`Static client assets not served because the build output was not found at "${clientDistPath}".`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
app.use(errorHandler);
|
app.use(errorHandler);
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ const rootDir = path.resolve(__dirname, '../../..');
|
|||||||
|
|
||||||
config({ path: path.resolve(rootDir, '.env') });
|
config({ path: path.resolve(rootDir, '.env') });
|
||||||
|
|
||||||
|
const resolvePath = (inputPath: string) =>
|
||||||
|
path.isAbsolute(inputPath) ? inputPath : path.resolve(rootDir, inputPath);
|
||||||
|
|
||||||
const port = Number(process.env.SERVER_PORT ?? process.env.PORT ?? 3000);
|
const port = Number(process.env.SERVER_PORT ?? process.env.PORT ?? 3000);
|
||||||
const urlReaderBaseUrl = (process.env.URL_READER_BASE_URL ?? 'https://r.jina.ai').replace(
|
const urlReaderBaseUrl = (process.env.URL_READER_BASE_URL ?? 'https://r.jina.ai').replace(
|
||||||
/\/$/,
|
/\/$/,
|
||||||
@@ -17,6 +20,8 @@ const urlReaderApiKey = process.env.URL_READER_API_KEY ?? '';
|
|||||||
const llmApiUrl = process.env.LLM_API_URL ?? '';
|
const llmApiUrl = process.env.LLM_API_URL ?? '';
|
||||||
const llmApiKey = process.env.LLM_API_KEY ?? '';
|
const llmApiKey = process.env.LLM_API_KEY ?? '';
|
||||||
const llmModel = process.env.LLM_MODEL ?? 'gpt-4o-mini';
|
const llmModel = process.env.LLM_MODEL ?? 'gpt-4o-mini';
|
||||||
|
const serveClient = process.env.SERVE_CLIENT !== 'false';
|
||||||
|
const clientDistPath = resolvePath(process.env.CLIENT_DIST_PATH ?? 'client/dist');
|
||||||
|
|
||||||
export const env = {
|
export const env = {
|
||||||
port,
|
port,
|
||||||
@@ -31,5 +36,9 @@ export const env = {
|
|||||||
apiUrl: llmApiUrl,
|
apiUrl: llmApiUrl,
|
||||||
apiKey: llmApiKey,
|
apiKey: llmApiKey,
|
||||||
model: llmModel
|
model: llmModel
|
||||||
|
},
|
||||||
|
client: {
|
||||||
|
serve: serveClient,
|
||||||
|
distPath: clientDistPath
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user