You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
import express from 'express'; |
|
import cors from 'cors'; |
|
import cookieParser from 'cookie-parser'; |
|
import dotenv from 'dotenv'; |
|
import authRoutes from './routes/auth.js'; |
|
import testsRoutes from './routes/tests.js'; |
|
import { RU } from './messages/ru.js'; |
|
|
|
dotenv.config(); |
|
|
|
export function createApp() { |
|
const app = express(); |
|
const corsOrigins = |
|
process.env.NODE_ENV === 'production' |
|
? process.env.FRONTEND_URL |
|
? [process.env.FRONTEND_URL] |
|
: [] |
|
: [ |
|
'http://localhost:3107', |
|
'http://localhost:3000', |
|
]; |
|
app.use( |
|
cors({ |
|
origin: corsOrigins.length ? corsOrigins : true, |
|
credentials: true, |
|
}) |
|
); |
|
app.use(express.json()); |
|
app.use(cookieParser()); |
|
app.use('/api/auth', authRoutes); |
|
app.use('/api/tests', testsRoutes); |
|
app.get('/api/health', (req, res) => { |
|
res.json({ |
|
status: 'ok', |
|
timestamp: new Date().toISOString(), |
|
message: 'Server is running', |
|
}); |
|
}); |
|
app.use((err, req, res, _next) => { |
|
console.error('Error:', err); |
|
res.status(err.status || 500).json({ |
|
error: err.message || RU.internal, |
|
}); |
|
}); |
|
app.use((req, res) => { |
|
res.status(404).json({ error: RU.notFound }); |
|
}); |
|
return app; |
|
}
|
|
|