115 lines
3.3 KiB
JavaScript
115 lines
3.3 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const mysql = require('mysql2/promise');
|
|
const productsRouter = require('./routes/products');
|
|
const dashboardRouter = require('./routes/dashboard');
|
|
const ordersRouter = require('./routes/orders');
|
|
const csvRoutes = require('./routes/csv');
|
|
|
|
const app = express();
|
|
|
|
// Debug middleware to log all requests
|
|
app.use((req, res, next) => {
|
|
console.log(`[App Debug] ${new Date().toISOString()} - ${req.method} ${req.path}`);
|
|
next();
|
|
});
|
|
|
|
// Configure CORS with specific options
|
|
app.use(cors({
|
|
origin: [
|
|
'http://localhost:5173', // Local development
|
|
'https://inventory.kent.pw', // Production frontend
|
|
/\.kent\.pw$/ // Any subdomain of kent.pw
|
|
],
|
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
allowedHeaders: ['Content-Type', 'Authorization'],
|
|
credentials: true,
|
|
optionsSuccessStatus: 200
|
|
}));
|
|
|
|
app.use(express.json());
|
|
|
|
// Database connection
|
|
const pool = mysql.createPool({
|
|
host: process.env.DB_HOST || 'localhost',
|
|
user: process.env.DB_USER || 'root',
|
|
password: process.env.DB_PASSWORD || '',
|
|
database: process.env.DB_NAME || 'inventory',
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0
|
|
});
|
|
|
|
// Make db pool available in routes
|
|
app.locals.pool = pool;
|
|
|
|
// Debug endpoint to list all registered routes
|
|
app.get('/api/debug/routes', (req, res) => {
|
|
console.log('Debug routes endpoint hit');
|
|
const routes = [];
|
|
app._router.stack.forEach(middleware => {
|
|
if (middleware.route) {
|
|
routes.push({
|
|
path: middleware.route.path,
|
|
methods: Object.keys(middleware.route.methods)
|
|
});
|
|
} else if (middleware.name === 'router') {
|
|
middleware.handle.stack.forEach(handler => {
|
|
if (handler.route) {
|
|
const fullPath = (middleware.regexp.source === '^\\/?(?=\\/|$)' ? '' : middleware.regexp.source.replace(/\\\//g, '/').replace(/\^|\$/g, '')) + handler.route.path;
|
|
routes.push({
|
|
path: fullPath,
|
|
methods: Object.keys(handler.route.methods)
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
res.json(routes);
|
|
});
|
|
|
|
// Test endpoint to verify server is running
|
|
app.get('/api/health', (req, res) => {
|
|
res.json({ status: 'ok' });
|
|
});
|
|
|
|
// Mount all routes under /api
|
|
console.log('Mounting routes...');
|
|
|
|
console.log('Mounting products routes...');
|
|
app.use('/api/products', productsRouter);
|
|
|
|
console.log('Mounting dashboard routes...');
|
|
app.use('/api/dashboard', dashboardRouter);
|
|
|
|
console.log('Mounting orders routes...');
|
|
app.use('/api/orders', ordersRouter);
|
|
|
|
console.log('Mounting CSV routes...');
|
|
app.use('/api/csv', csvRoutes);
|
|
console.log('CSV routes mounted');
|
|
|
|
console.log('All routes mounted');
|
|
|
|
// Error handling middleware
|
|
app.use((err, req, res, next) => {
|
|
console.error('Error:', err);
|
|
res.status(500).json({ error: err.message });
|
|
});
|
|
|
|
// 404 handler
|
|
app.use((req, res) => {
|
|
console.log('404 Not Found:', req.method, req.path);
|
|
res.status(404).json({ error: 'Not Found' });
|
|
});
|
|
|
|
const PORT = process.env.PORT || 3010;
|
|
app.listen(PORT, () => {
|
|
console.log(`Server is running on port ${PORT}`);
|
|
console.log('Available routes:');
|
|
console.log('- GET /api/health');
|
|
console.log('- GET /api/debug/routes');
|
|
console.log('- GET /api/csv/status');
|
|
console.log('- GET /api/csv/test');
|
|
console.log('- POST /api/csv/update');
|
|
});
|