NextNodeTemplate/backend/server.js

44 lines
1.2 KiB
JavaScript

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = process.env.PORT || 3001;
// Middleware
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();
}
});
// Тестовый endpoint
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', message: 'Server is running' });
});
// Пример 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 });
}
});
// Запуск сервера
app.listen(PORT, () => {
console.log(`🚀 Server is running on http://localhost:${PORT}`);
console.log(`📡 Health check: http://localhost:${PORT}/api/health`);
});