dmrocket_api/src/app.ts
2025-07-14 23:36:22 +05:30

62 lines
1.5 KiB
TypeScript

import express, { Request, Response } from 'express';
import dotenv from 'dotenv';
import { testConnection } from './config/database';
import orderRoutes from './routes/orderRoutes';
// Load environment variables
dotenv.config({ path: '.env.local' });
const app = express();
const port: number = 7111;
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Test database connection on startup
testConnection().then((connected) => {
if (!connected) {
console.error('Failed to connect to database. Please check your .env.local configuration.');
}
});
// Routes
app.get('/', (req: Request, res: Response) => {
res.send('DMRocket API - Unauthorized');
});
// API Routes
app.use('/api/v1', orderRoutes);
// Health check endpoint
app.get('/health', (req: Request, res: Response) => {
res.json({
status: 'OK',
timestamp: new Date().toISOString(),
database: 'Connected'
});
});
// Error handling middleware
app.use((err: any, req: Request, res: Response, next: any) => {
console.error('Error:', err);
res.status(500).json({
error: 'Internal server error',
message: err.message || 'Something went wrong'
});
});
// 404 handler
app.use('*', (req: Request, res: Response) => {
res.status(404).json({
error: 'Not found',
message: 'The requested endpoint does not exist'
});
});
// Start server
app.listen(port, () => {
console.log(`DMRocket API is listening at http://localhost:${port}`);
console.log(`Health check available at http://localhost:${port}/health`);
});