61 lines
1.5 KiB
TypeScript

// src/app.ts
import express from 'express';
import cors from 'cors';
import config from './config/config';
import routes from './routes';
import { errorHandler } from './middleware';
import logger from './utils/logger';
const app = express();
// Debug middleware to log all incoming requests
app.use((req, res, next) => {
logger.debug(`Incoming request: ${req.method} ${req.originalUrl}`);
logger.debug('Request body:', req.body);
next();
});
// CORS configuration
app.use(cors({
origin: ['http://localhost:5173', 'http://localhost:3000'],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
app.use(express.json());
// Routes
logger.info(`Registering routes with prefix: ${config.server.apiPrefix}`);
app.use(config.server.apiPrefix, routes);
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'OK', timestamp: new Date().toISOString() });
});
// Error handling
app.use(errorHandler);
// 404 handler - Debug version
app.use((req, res) => {
logger.warn('404 Not Found:', {
method: req.method,
path: req.path,
originalUrl: req.originalUrl,
body: req.body
});
res.status(404).json({
error: 'Not Found',
path: req.path,
originalUrl: req.originalUrl
});
});
const port = config.server.port || 3000;
app.listen(port, () => {
logger.info(`Server running on port ${port} in ${config.env} mode`);
logger.info(`API endpoints available at http://localhost:${port}${config.server.apiPrefix}`);
});
export default app;