53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import express, { Express, Request, Response } from 'express';
|
|
import cors from 'cors';
|
|
import helmet from 'helmet';
|
|
import morgan from 'morgan';
|
|
import dotenv from 'dotenv';
|
|
import { errorHandler } from './middleware/errorHandler';
|
|
import { notFound } from './middleware/notFound';
|
|
import apiRoutes from './routes';
|
|
import { runMigrations } from './db/migrate';
|
|
|
|
dotenv.config();
|
|
|
|
const app: Express = express();
|
|
const port = process.env.PORT || 3001;
|
|
|
|
// Middleware
|
|
app.use(helmet());
|
|
app.use(cors({
|
|
origin: process.env.CORS_ORIGIN || ['http://localhost:5173', 'https://sm.jiosii.com'],
|
|
credentials: true
|
|
}));
|
|
app.use(morgan('dev'));
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Health check
|
|
app.get('/health', (req: Request, res: Response) => {
|
|
res.json({
|
|
status: 'ok',
|
|
timestamp: new Date().toISOString(),
|
|
uptime: process.uptime()
|
|
});
|
|
});
|
|
|
|
// API Routes
|
|
app.use('/api', apiRoutes);
|
|
|
|
// Error handling
|
|
app.use(notFound);
|
|
app.use(errorHandler);
|
|
|
|
async function start() {
|
|
await runMigrations();
|
|
app.listen(port, () => {
|
|
console.log(`⚡️ Server is running on port ${port}`);
|
|
console.log(`🌍 Environment: ${process.env.NODE_ENV || 'development'}`);
|
|
});
|
|
}
|
|
|
|
start().catch(console.error);
|
|
|
|
export default app;
|