Reflex/backend/routes/userRoutes.js
2025-05-21 22:13:09 +07:00

47 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const { updateUserProfile, getUsersForSwiping, uploadUserProfilePhoto } = require('../controllers/userController');
const { protect } = require('../middleware/authMiddleware'); // Нам нужен protect для защиты маршрута
const multer = require('multer'); // 1. Импортируем multer
const path = require('path'); // Может понадобиться для фильтрации файлов
// Настройка multer для загрузки фотографий
const storage = multer.memoryStorage(); // Хранить файл в памяти, а не на диске
// Конфигурация multer с проверкой типа файла
const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
// Проверяем, является ли файл изображением
const filetypes = /jpeg|jpg|png|webp/;
const mimetype = filetypes.test(file.mimetype);
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
} else {
// cb(new Error('Допустимы только изображения (jpg, jpeg, png, webp)!'), false);
const err = new Error('Разрешены только изображения (jpg, jpeg, png, webp)!'); // Изменено сообщение об ошибке
err.statusCode = 400; // Устанавливаем statusCode для multer ошибок
cb(err, false);
}
},
limits: {
fileSize: 5 * 1024 * 1024, // Ограничение размера файла (5 МБ)
},
});
// Маршрут для обновления профиля текущего аутентифицированного пользователя
// PUT /api/users/profile
router.put('/profile', protect, updateUserProfile);
router.get('/suggestions', protect, getUsersForSwiping); // <--- НОВЫЙ МАРШРУТ
// Маршрут для загрузки фотографии профиля
// POST /api/users/profile/photo
router.post('/profile/photo', protect, upload.single('profilePhoto'), uploadUserProfilePhoto);
// Маршрут для получения профиля по ID (например, для просмотра чужих профилей, если это нужно)
// GET /api/users/:id
// router.get('/:id', protect, getUserById); // protect здесь может быть опционален или с другой логикой
module.exports = router;