path correction
This commit is contained in:
+35
-14
@@ -2,6 +2,7 @@ import 'dotenv/config';
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { initDb } from './db.js';
|
||||
import authRoutes from './routes/auth.js';
|
||||
@@ -12,6 +13,13 @@ import multer from 'multer';
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const UPLOAD_DIR = process.env.UPLOAD_DIR || path.join(__dirname, 'uploads');
|
||||
// Public URL prefix on Apache (e.g. /citpl_website). Leave empty if site is at domain root.
|
||||
const SITE_BASE = (process.env.SITE_BASE || '/citpl_website').replace(/\/$/, '');
|
||||
const distDir = path.join(__dirname, '..', 'dist');
|
||||
const serveStatic =
|
||||
process.env.SERVE_STATIC === '1' ||
|
||||
process.env.SERVE_STATIC === 'true' ||
|
||||
(process.env.SERVE_STATIC !== '0' && fs.existsSync(distDir));
|
||||
|
||||
initDb();
|
||||
|
||||
@@ -23,22 +31,30 @@ app.use(cors({
|
||||
}));
|
||||
|
||||
app.use(express.json({ limit: '10mb' }));
|
||||
app.use('/uploads', express.static(UPLOAD_DIR));
|
||||
|
||||
app.get('/api/health', (_req, res) => {
|
||||
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
||||
});
|
||||
function mountApp(base) {
|
||||
const prefix = base || '';
|
||||
|
||||
app.use('/api/auth', authRoutes);
|
||||
app.use('/api/content', contentRoutes);
|
||||
app.use('/api/upload', uploadRoutes);
|
||||
app.use(`${prefix}/uploads`, express.static(UPLOAD_DIR));
|
||||
|
||||
// Optional: serve Vite dist from Node (set SERVE_STATIC=1). Useful when Apache
|
||||
// proxies the whole /citpl_website path to this process.
|
||||
if (process.env.SERVE_STATIC === '1') {
|
||||
const distDir = path.join(__dirname, '..', 'dist');
|
||||
app.use(express.static(distDir));
|
||||
app.use('/citpl_website', express.static(distDir));
|
||||
app.get(`${prefix}/api/health`, (_req, res) => {
|
||||
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
||||
});
|
||||
|
||||
app.use(`${prefix}/api/auth`, authRoutes);
|
||||
app.use(`${prefix}/api/content`, contentRoutes);
|
||||
app.use(`${prefix}/api/upload`, uploadRoutes);
|
||||
|
||||
if (serveStatic) {
|
||||
app.use(prefix || '/', express.static(distDir));
|
||||
}
|
||||
}
|
||||
|
||||
// Root paths — used when Apache proxies /citpl_website/api → http://127.0.0.1:3001/api
|
||||
mountApp('');
|
||||
// Prefixed paths — used when Apache proxies /citpl_website → http://127.0.0.1:3001/citpl_website
|
||||
if (SITE_BASE) {
|
||||
mountApp(SITE_BASE);
|
||||
}
|
||||
|
||||
app.use((err, _req, res, _next) => {
|
||||
@@ -50,5 +66,10 @@ app.use((err, _req, res, _next) => {
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`API server running on http://localhost:${PORT}`);
|
||||
console.log(`CITPL Node server on http://localhost:${PORT}`);
|
||||
console.log(` API: /api/* and ${SITE_BASE || ''}/api/*`);
|
||||
console.log(` Auth: ADMIN_USERNAME from .env (${process.env.ADMIN_USERNAME || 'admin'})`);
|
||||
if (serveStatic) {
|
||||
console.log(` Static: ${distDir}`);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user