Get data loading in frontend

This commit is contained in:
2025-01-09 17:08:46 -05:00
parent 608f376790
commit 9034897ef1
9 changed files with 253 additions and 201 deletions

View File

@@ -2,7 +2,7 @@ const path = require('path');
const fs = require('fs');
// Get the absolute path to the .env file
const envPath = path.resolve('/var/www/html/inventory/.env');
const envPath = path.resolve(process.cwd(), '.env');
console.log('Current working directory:', process.cwd());
console.log('Looking for .env file at:', envPath);
console.log('.env file exists:', fs.existsSync(envPath));
@@ -10,6 +10,13 @@ console.log('.env file exists:', fs.existsSync(envPath));
try {
require('dotenv').config({ path: envPath });
console.log('.env file loaded successfully');
console.log('Environment check:', {
NODE_ENV: process.env.NODE_ENV || 'not set',
PORT: process.env.PORT || 'not set',
DB_HOST: process.env.DB_HOST || 'not set',
DB_USER: process.env.DB_USER || 'not set',
DB_NAME: process.env.DB_NAME || 'not set',
});
} catch (error) {
console.error('Error loading .env file:', error);
}
@@ -39,15 +46,18 @@ const dashboardRouter = require('./routes/dashboard');
const app = express();
// CORS configuration
// CORS configuration - move before route handlers
app.use(cors({
origin: ['https://inventory.kent.pw', 'https://www.inventory.kent.pw'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true
credentials: true,
optionsSuccessStatus: 200 // Some legacy browsers (IE11) choke on 204
}));
// Body parser middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Request logging middleware
app.use((req, res, next) => {
@@ -61,6 +71,16 @@ app.use((req, res, next) => {
next();
});
// Error handling middleware - move before route handlers
app.use((err, req, res, next) => {
console.error(`[${new Date().toISOString()}] Error:`, err);
res.status(500).json({
error: process.env.NODE_ENV === 'production'
? 'An internal server error occurred'
: err.message
});
});
// Database connection pool
const pool = mysql.createPool({
host: process.env.DB_HOST,
@@ -101,16 +121,6 @@ app.get('/health', (req, res) => {
});
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(`[${new Date().toISOString()}] Error:`, err);
res.status(500).json({
error: process.env.NODE_ENV === 'production'
? 'An internal server error occurred'
: err.message
});
});
// Handle uncaught exceptions
process.on('uncaughtException', (err) => {
console.error(`[${new Date().toISOString()}] Uncaught Exception:`, err);