66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const logger = require('./middleware/logger');
|
|
const errorHandler = require('./middleware/errorHandler');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3001;
|
|
|
|
// Middleware
|
|
app.use(logger);
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
// CORS (настройте под свои нужды)
|
|
app.use((req, res, next) => {
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
|
|
if (req.method === 'OPTIONS') {
|
|
res.sendStatus(200);
|
|
} else {
|
|
next();
|
|
}
|
|
});
|
|
|
|
// Health check endpoint
|
|
app.get('/api/health', (req, res) => {
|
|
res.json({
|
|
status: 'ok',
|
|
message: 'Server is running',
|
|
timestamp: new Date().toISOString(),
|
|
});
|
|
});
|
|
|
|
// Пример API endpoint
|
|
app.post('/api/example', (req, res) => {
|
|
try {
|
|
const { data } = req.body;
|
|
res.json({ success: true, received: data });
|
|
} catch (err) {
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
// Подключение роутов (пример)
|
|
// const exampleRoutes = require('./routes/example');
|
|
// app.use('/api/example', exampleRoutes);
|
|
|
|
// Обработка 404
|
|
app.use((req, res) => {
|
|
res.status(404).json({
|
|
success: false,
|
|
error: 'Route not found',
|
|
});
|
|
});
|
|
|
|
// Обработка ошибок (должен быть последним middleware)
|
|
app.use(errorHandler);
|
|
|
|
// Запуск сервера
|
|
app.listen(PORT, () => {
|
|
console.log(`🚀 Server is running on http://localhost:${PORT}`);
|
|
console.log(`📡 Health check: http://localhost:${PORT}/api/health`);
|
|
});
|
|
|